Binding to a service that lives in the Xamarin Bound Java library

I have an imported Java library which is the "Bindings Library" project in my solution.

I am trying to bind to a service that lives in this third party library from another project in a solution.

The third party library documentation [in java] is pretty simple:

Declare MeshService object in Activity class:

private MeshService mService;

      

Bind to service inside onCreate:

Intent bindIntent = new Intent(this, MeshService.class);
bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);

      

When I try to link using the following code:

Intent bindIntent = new Intent(this, typeof(MeshService));
mServiceConnection = new ServiceConnection(this);
BindService(bindIntent, mServiceConnection, Bind.AutoCreate);

      

There is an exception in 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 try to link 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 null. (see code below)

Question: How do I bind a service to 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;
        }
    }

      

Thank you so much for any advice on this!

also posted on the xamarin forums here: https://forums.xamarin.com/discussion/44647/binding-to-a-service-that-lives-in-a-bound-java-library?new=1

+3


source to share


1 answer


It seems that your linking to your library is not working correctly, otherwise you will be able to see the AutoCompletion suggestions and not get such errors. Checkout Article . It details how to link the Android library.



0


source







All Articles