Java Native Invoke Sample
=========================
This sample shows how to manually bind to a Java library so it can
be consumed by a Mono for Android application.
Note this binds to the Google Maps library as a sample, but Mono for Android
provides bindings to the Google Maps library for you. If you just want
to use Maps, see the MapsDemo sample instead.
Requirements
------------
There are three requirements in order to build and run this sample:
1. Mono for Android Preiew 13 or later.
2. The Google APIs Android SDK add-on.
3. A device with Google Maps support.
4. A keystore file.
5. Obtain an API Key for use with Google Maps.
Commands that need to be executed are indicated within backtics (`),
and $ANDROID_SDK_PATH is the directory that contains your Android SDK
installation.
Installing the Google APIs Android SDK add-on
---------------------------------------------
To install the Google APIs Android SDK add-on:
1. Launch the Android SDK adn AVD manager:
`$ANDROID_SDK_PATH/tools/android`
2. Within the Android SDK and AVD manager, click Available packages
in the left-hand pane.
3. In the right-hand pane, navigate to the tree view node Third party
Add-ons /Google Inc. add-ons (dl-ssl.google.com)
4. Select the check-box for "Google APIs by Google Inc., Android API
8, revision 2".
5. Click the Install Selected button in the lower right corner.
6. In the "Choose Packages to Install" dialog, select the Accept
radio button, then click the Install button.
Creating a device with Google Maps support
------------------------------------------
To verify that your target device has Google Maps support, you can use
`$ANDROID_SDK_PATH/platform-tools/adb shell ls /system/framework/*map*`
to see if Google Maps support is present. It should be present on
hardware devices, but may not be present within some emulators.
If you need an emulator with Google Maps support:
1. Launch the Android SDK adn AVD manager:
`$ANDROID_SDK_PATH/tools/android`
2. Within the Android SDK and AVD manager, click Virtual devices
in the left-hand pane.
3. Click the New... button on the right-hande side.
4. In the Create new Andorid Virtual Device (AVD) dialog, provide a
name for the device (e.g. MAPS), and in the Target drop-down
select the Google APIs (Google Inc.) - API Level 8 entry.
5. Click the Create AVD button.
You may now launch the emulator with:
`$ANDROID_SDK_PATH/tools/emulator -partition-size 512 -avd MAPS`
Keystore
--------
The Google Maps API key is based on the `keytool`-generated keystore.
Consequently, you must use the same certificate every time you sign the
application. Since the default Mono for Android build process always
creates a new debug keystore (and one only good for 6 months, and thus not
suitable for use on the Android Market), we need to alter the default
behavior.
This involves two steps:
1. Creating the keystore to use.
2. Altering the build process to use the new keystore.
To simplify matters, this project includes a `public.keystore` file
within the repository, but this is likely not a good idea for
production apps:
http://developer.android.com/guide/publishing/app-signing.html#cert
The `public.keystore` file was generated using the command:
`keytool -genkey -v -keystore public.keystore -alias public -keyalg RSA -keysize 2048 -validity 10000`
Once you have a keystore to use, you need to tell Mono for Android to
use your keystore to sign the final package instead of using the
default debug keystore. This requires modifying the .csproj in
accordance with:
http://mono-android.net/Documentation/Build_Process#Signing
The required build system changes have already been done for `GoogleMaps.csproj`:
<PropertyGroup>
<AndroidKeyStore>True</AndroidKeyStore>
<AndroidSigningKeyStore>public.keystore</AndroidSigningKeyStore>
<AndroidSigningStorePass>public</AndroidSigningStorePass>
<AndroidSigningKeyAlias>public</AndroidSigningKeyAlias>
<AndroidSigningKeyPass>public</AndroidSigningKeyPass>
</PropertyGroup>
Again, for actual non-sample apps, it may not be advisable to have the
private keystore within source control, as is done for this sample.
Google Maps API Key
-------------------
See the Google Maps sample/tutorial at
http://mobiforge.com/developing/story/using-google-maps-android
for more information about installing and using the API key. You will
also need to edit the file Resources\Layout\Map.axml and edit the
/RelativeLayout/com.google.android.maps.MapView/@android:apiKey
attribute to contain your Google Maps API Key.
If you don't change this, you'll get a blank map, though the sample
will still serve to demonstrate how C# and Java interop can work.
How it works
------------
As Mono for Android 1.0 does not support binding arbitrary .jar
files (only the Android SDK android.jar is bound), alternative
mechanisms must instead used for interoperation between Java and
managed code. Two primary mechanisms are:
1. Android.Runtime.JNIEnv for creating instances of Java objects and
invoking methods on them from C#. This is very similar to
System.Reflection in that everything is string based and thus
untyped at compile time.
2. The ability to include Java source code and .jar files into the
resulting Android application.
Use of the Google Maps API requires two things:
1. com.google.android.maps.MapActivity MUST be subclassed and the
MapActivity.isRouteDisplayed() method overridden.
2. The MapActivity subclass must be created BEFORE creating any
MapView instances, AND the MapView instances can ONLY be added as
children of the MapActivity.
Because we need to subclass an unbound Java type (1), we will need to
write custom Java code, thus necessitating Mechanism (2).
The Java source code is kept in MyMapActivity.java, which is included
in the project with a Build Action of AndroidJavaSource.
Furthermore, we edit Properties\AndroidManifest.xml so that it
contains three additional elements:
1. /manifest/uses-permission must be added to include the
android.permission.INTERNET permission. This can be done within
the IDE, if desired.
2. /manifest/application/uses-library must be added to specify that
the com.google.android.maps library will be used.
3. A /manifest/application/activity element must be created so that
we can use Context.StartActivity() to create the custom activity.
This translates to having the following XML within
AndroidManifest.xml:
<application android:label="Managed Maps">
<uses-library android:name="com.google.android.maps" />
<activity android:name="mono.samples.googlemaps.MyMapActivity" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
MyMapActivity.java uses the Resources\Layout\Map.axml resource, which
contains a com.google.android.maps.MapView instance.
Resource.Layout.Map CANNOT be used from Activity1.cs, as MapView
requires that the constructing Activity be a MapActivity subclass.
Instead, Activity1.cs uses Java.Lang.Class.FindClass() to obtain a
Java.Lang.Class instance for the custom MyMapActivity type, then we
create an Intent to pass to Activity.StartActivity() to launch the
MapActivity subclass.
The <uses-library/> element is used by monodroid.exe to lookup the
appropriate Android SDK Add-on and include the .jar file in the Java
source compilation so that everything compiles properly. No
additional .jar files need to be specified.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
C# mono例子 (2344个子文件)
gesturebuilder.ai 357KB
IAdditionService.aidl 106B
Main.axml 5KB
AlertDialog.axml 3KB
Main.axml 3KB
ActivityHome.axml 3KB
LinearLayoutTutorial.axml 3KB
ActionBarTabs.axml 2KB
ActivityHome.axml 2KB
ActionBarDisplayOptions.axml 2KB
notification.axml 2KB
device_list.axml 2KB
main.axml 2KB
FragmentDashboard.axml 2KB
TableLayoutTutorial.axml 2KB
main.axml 2KB
FormWidgetsTutorial.axml 2KB
ActivityHome.axml 2KB
custom_title.axml 1KB
ActivityRecreate.axml 1KB
main.axml 1KB
Main.axml 1KB
main.axml 1KB
ActivityHome.axml 1KB
ContactManager.axml 1KB
ListItem.axml 1KB
fragment_webview_with_spinner.axml 1KB
main.axml 1KB
content_welcome.axml 1KB
Main.axml 1KB
Main.axml 1KB
MapLayout.axml 1KB
menu.axml 1KB
RelativeLayoutTutorial.axml 1KB
NoteEditor.axml 1KB
camera_sample.axml 1KB
ContactEntry.axml 1KB
Main.axml 1KB
Main.axml 1011B
title_list_item.axml 999B
MapLayout.axml 951B
action_bar_custom.axml 943B
BouncingBalls.axml 913B
NoteListRow.axml 893B
FragmentWhatsOn.axml 878B
message.axml 876B
device_name.axml 876B
ActionBarTabContent.axml 868B
ActionBarDisplayOptionsCustom.axml 801B
Main.axml 795B
Main.axml 788B
DisplayHominid.axml 775B
TabLayoutTutorial.axml 744B
LocationView.axml 715B
LocationView.axml 715B
activity_main.axml 650B
activity_main.axml 649B
SpinnerTutorial.axml 647B
Main.axml 640B
Main.axml 635B
EventListItem.axml 601B
Main.axml 599B
CalListItem.axml 597B
AutoCompleteTutorial.axml 591B
EventList.axml 579B
TimePickerTutorial.axml 560B
DatePickerTutorial.axml 560B
Main.axml 551B
Main.axml 545B
Main.axml 545B
Map.axml 543B
Main.axml 532B
SimpleStateView.axml 530B
Main.axml 504B
Main.axml 491B
Second.axml 476B
Main.axml 468B
Main.axml 460B
MapWithOverlayLayout.axml 456B
Main.axml 442B
Main.axml 436B
activity_main.axml 436B
activity_main.axml 435B
GridViewTutorial.axml 432B
Main.axml 424B
Main.axml 419B
Main.axml 416B
ListItem.axml 415B
Main.axml 412B
Main.axml 412B
Main.axml 412B
Main.axml 411B
ListHeader.axml 407B
CalendarList.axml 404B
MapWithOverlayLayout.axml 390B
Main.axml 386B
Main.axml 374B
main.axml 314B
list_item.axml 288B
Main.axml 274B
共 2344 条
- 1
- 2
- 3
- 4
- 5
- 6
- 24
资源评论
- wangyonglei2013-10-07不错的资料,要是mono免费就好了.
liuhongwei90
- 粉丝: 38
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- KIMI大模型浏览器插件
- b61fa64a08a02de0e0d49d53bb84c444.amr
- 5ffd9193f6aec31bbf16030a46680dc7.avi
- DA14531-蓝牙传感器连接传输数据固件
- logisim实验MIPS运算器(ALU)设计(内含4位先行进位74182、四位快速加法器、32位快速加法器)-Educoder_logisim里面连线,实现4位先行进位74182和4位快速加法器-C
- 高等数学第一章第二节数列的极限
- Python 版冒泡排序算法源代码
- 基于YOLOv8的教室人脸识别 附源码 预训练模型
- 基于JAVA实现的用于全局监控网络请求错误并记录日志的library
- tensorflow-gpu-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功