Binding to a Service that lives in a Bound Java Library

Hi there!

I have an imported Java Library, which is a "Bindings Library" project in my solution.

I'm trying to Bind to the service that lives in that 3rd party Library from another project in the solution.

3rd party Library documentation [in java] is pretty straight forward:

Declare the MeshService object in the Activity class:
private MeshService mService;
Bind to the service inside onCreate:
Intent bindIntent = new Intent(this, MeshService.class);
bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);

When I'm trying to bind using the following code:
Intent bindIntent = new Intent(this, typeof(MeshService));
mServiceConnection = new ServiceConnection(this);
BindService(bindIntent, mServiceConnection, Bind.AutoCreate);

There is an exception thrown on the first line
Java.Lang.ClassNotFoundException: Didn't find class "com.csr.mesh.MeshService" on path:DexPathList[[zip file "/data/app/DeakoMesh.Android-1/base.apk"],nativeLibraryDirectories=[/data/app/DeakoMesh.Android-1/lib/arm, /system/lib, /vendor/lib, system/vendor/lib, system/vendor/lib/egl, system/lib/hw]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)

When I'm trying to bind using another piece of code:
Intent bindIntent = new Intent("com.csr.mesh.MeshService");
mServiceConnection = new ServiceConnection(this);
BindService(bindIntent, mServiceConnection, Bind.AutoCreate);

there is no exception, but service binding never happens and mServiceConnection is a null. (please see code below)

Question:
How do I bind to a service in another project ?
What context should I provide instead of "this" ?
Intent bindIntent = new Intent(this, typeof(MeshService));

Code for mServiceConnection:
class ServiceConnection : Java.Lang.Object, IServiceConnection
{
MainActivity activity;

        public ServiceConnection(MainActivity activity)
        {
            this.activity = activity;
        }

        public void OnServiceConnected(ComponentName name, IBinder service)
        {
            activity.meshService = ((MeshService.LocalBinder)service).Service;                
            activity.isBound = true;
        }

        public void OnServiceDisconnected(ComponentName name)
        {
            activity.meshService = null;
            activity.isBound = false;
        }
    }

Huge thanks in advance for any tips on this!

Posts

  • AntaoAlmadaAntaoAlmada PTMember ✭✭

    Any luck with this?
    I tried using Java.Lang.Class.ForName() with the Java class name but I always get a System.Exception: Service bind failed!
    I also set the build action of the .jar to EmbeddedJar to make sure it's present but no luck either.

  • AntaoAlmadaAntaoAlmada PTMember ✭✭

    What I found until now:

    • The use of Java.Lang.Class.ForName() for the Intent is not required. typeof() sets the correct class name as the managed class has a RegisterAttribute with the Java class name.
    • Tried using both the Xamarin.Forms.Forms.Context and Android.App.Application.Context as the context.
    • Context.BindService() always returns false.
    • Context.StartService() using the exact same Intent works fine.

    NOTE: The exception I reported on the previous post is because I was using ReactiveUI and the exception is thrown when BindService() returns false.

  • AntaoAlmadaAntaoAlmada PTMember ✭✭

    Ups, I got it wrong. Context.StartService() also doesn't work. It returns null.
    I forgot to mention I'm using Xamarin.Forms and Xamarin.Forms.Forms.Context gets me the Activity context.

  • AFeldeAFelde DEMember ✭✭

    I have exact the same problem, but I get an invalidCastException.
    Have you solved your problem? If yes, could you provide more Information.

    I don't even know if it possible to bind to 3rd party Library Service

  • AFeldeAFelde DEMember ✭✭

    @AFelde said:
    I have exact the same problem, but I get an invalidCastException.
    Have you solved your problem? If yes, could you provide more Information.

    I don't even know if it possible to bind to 3rd party Library Service

    Ok, I found my Problem
    And the first thing is, Yes, it is possible to bind to 3rd party Library Service to your Application,
    and my problem was that my ServiceConnection hasn't implemented the Java.Lang.Object

Sign In or Register to comment.