2015-06-30 56 views
3

我有一个导入的Java库,它是我的解决方案中的“绑定库”项目。绑定到位于Xamarin绑定Java库中的服务

我试图绑定到解决方案中另一个项目中的第三方库中的服务。

第三方库文件[在Java]是非常简单的:

声明在活动类的MeshService对象:

private MeshService mService; 

绑定到里面的onCreate服务:

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

当我尝试使用以下代码进行绑定时:

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

有扔在了第一线

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) 

当我试图用另一段代码来绑定一个例外:

Intent bindIntent = new Intent("com.csr.mesh.MeshService"); 
mServiceConnection = new ServiceConnection(this); 
BindService(bindIntent, mServiceConnection, Bind.AutoCreate); 

没有什么异常,但服务绑定从未发生过而mServiceConnection是空的。 (请参阅下面的代码)

问题: 如何绑定到另一个项目中的服务? 我应该提供什么样的环境而不是“这个”?

Intent bindIntent = new Intent(this, typeof(MeshService)); 

代码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; 
     } 
    } 

非常非常感谢有这方面的技巧!

还张贴在这里xamarin论坛: https://forums.xamarin.com/discussion/44647/binding-to-a-service-that-lives-in-a-bound-java-library?new=1

回答

0

看来你绑定到库不能正常工作,否则你应该能够看到的AutoCompletion的建议,并不会得到这样的错误。 结帐此Article。这里详细解释了如何绑定Android库