[mono-android] Googlemap sample and Java -> MDr interop
Jonathan Pryor
jpryor at novell.com
Sat Mar 19 19:26:45 EDT 2011
On Mar 19, 2011, at 4:25 PM, Yann Schwartz wrote:
> I could write everything in Java *shudder* or have a nicely populated ArrayList<OverlayItem> sent from a .NET class instance (or from a static member of a .NET class, I'm not being difficult here) where the grunt work will be done.
It'll be ugly, but you could create such a list in managed code by using Android.Runtime.JavaList<Java.Lang.Object> (which is implemented as a java.util.ArrayList), then using JNIEnv to create OverlayItem instances and set fields/etc:
var list = new JavaList<Java.Lang.Object>();
IntPtr OverlayItem = JNIEnv.FindClass("com/google/android/maps/OverlayItem");
IntPtr ctor = JNIEnv.GetMethodId (OverlayItem, "<init>", "(...)V"); // TODO: prototype
// ...
IntPtr instance = JNIEnv.NewObject(OverlayItem, ctor /* ctor args... */);
list.Add(new Java.Lang.Object(instance));
See also: http://mono-android.net/Documentation/API_Design#Jave_Native_Interface_Support
You could then pass `list` via JNI to some Java method...
> So JNI is the way to go when you want to call Java from Monodroid. But how do I get access to the monodroid Java proxies so I can call .NET instances *from* java code?
Magically. :-)
Specifically, the proxies are generated under e.g. obj/Debug/android/src, and they're "normal" Java types (with lots of 'native' methods).
The problem is that the proxies only declare overridden methods and implemented interface methods, not any "random" method that is declared within managed code. (This will change in the future, but not before 1.0.) Consequently, you'd have to find a method (non-final class & method, or interface method) within Mono.Android that you can "abuse" for this purpose, then invoke from Java code.
For example, you could have a C# type implement Java.Lang.IRunnable, and when IRunnable.Run() is invoked, have it create all the data and invoke a static method within Java code, providing it the list.
Alternatively (if you need to pass parameters from Java to managed code), you could subclass Activity, stuff the extra parameters within the Intent and call Activity.startActivity() to load the managed class, and have the managed code "do something" then call Activity.Finish() (so that it doesn't display a UI).
- Jon
More information about the Monodroid
mailing list