28

I'm currently trying to implement an ILocationListener on a class in order to receive GPS updates - this is not on an Activity, just a normal C# class.

As part of the ILocationListener contract I need to support I JavaObject:

    public IntPtr Handle
    {
        get { throw new NotImplementedException(); }
    }

What should I do for this?

Also, are there any good documents and/or blog posts out there which talk about how C# classes and Java objects are bound together in MonoDroid - if I can find those documents, then I probably won't need to ask questions like this and I'll hopefully also be writing much better code.

Thanks

Stuart
  • 65,726
  • 7
  • 109
  • 161

3 Answers3

55

Taken from Xamarin's docs:

There are times when you may need to implement an Android interface, such as Android.Content.IComponentCallbacks. Since all Android classes and interface extend the Android.Runtime.IJavaObject interface, the question arises: how do we implement IJavaObject?

The question was answered above: the reason all Android types need to implement IJavaObject is so that MonoDroid has an Android callable wrapper to provide to Android, i.e. a Java proxy for the given type. Since monodroid.exe only looks for Java.Lang.Object subclasses, and Java.Lang.Object implements IJavaObject, the answer is obvious: subclass Java.Lang.Object

Basically you should always inherit from Java.Lang.Object in these cases, as implementing IJavaObject yourself won't actually work since Mono for Android won't generate the callable wrapper.

If it helps, I have an example implementing ILocationListener available here. It implements it directly on an activity, but you can inherit from Java.Lang.Object instead of Activity.

Community
  • 1
  • 1
Greg Shackles
  • 9,829
  • 1
  • 27
  • 35
  • 3
    For people who still didn't understand he simply means : "public class locationManager : Java.Lang.Object, ILocationListener" – Rahul Jha May 06 '16 at 18:01
2

I think I've found the first piece of the puzzle myself - it seems like there is a Java.Lang.Object handy and I can use that.

I'll continue to look for the binding information - hopefully I'll then know what I'm doing :)

Stuart
  • 65,726
  • 7
  • 109
  • 161
0

This also solved my Seg Fault crash [mono-rt] Got a SIGSEGV while executing native code..... problems that was being caused by a Class that implemented IJavaObject!

Ajay Chebbi
  • 171
  • 8