我定义了一个本地服务:
public class ComService extends Service implements IComService { private IBinder binder = new ComServiceBinder(); public class ComServiceBinder extends Binder implements IComService.IServiceBinder { public IComService getService() { return ComService.this; } } public void test(String msg) { System.out.println(msg); } @Override public IBinder onBind(Intent intent) { return binder; } }相应的界面:
public interface IComService { public void test(String msg); public interface IServiceBinder { IComService getService(); } }然后,我尝试在另一个应用程序中的另一个活动中绑定该服务,其中提供了相同的接口:
bindService(new Intent("ch.ifi.csg.games4blue.gamebase.api.ComService"), conn, Context.BIND_AUTO_CREATE);和
private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i("INFO", "Service bound " + name); comService = ((IComService.IServiceBinder)service).getService(); serviceHandler.sendEmptyMessage(0); } @Override public void onServiceDisconnected(ComponentName arg0) { Log.i("INFO", "Service Unbound "); } };但是行
comService = ((IComService.IServiceBinder)service).getService();总是抛出一个
05-02 22:12:55.922: ERROR/AndroidRuntime(622): java.lang.ClassCastException: android.os.BinderProxy我无法解释为什么,我遵循http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalServiceBinding.html上的应用示例
任何提示都会很好!
I defined a local Service:
public class ComService extends Service implements IComService { private IBinder binder = new ComServiceBinder(); public class ComServiceBinder extends Binder implements IComService.IServiceBinder { public IComService getService() { return ComService.this; } } public void test(String msg) { System.out.println(msg); } @Override public IBinder onBind(Intent intent) { return binder; } }The corresponding interface:
public interface IComService { public void test(String msg); public interface IServiceBinder { IComService getService(); } }Then i try to bind the service in another activity in another application, where the same interface is available:
bindService(new Intent("ch.ifi.csg.games4blue.gamebase.api.ComService"), conn, Context.BIND_AUTO_CREATE);and
private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i("INFO", "Service bound " + name); comService = ((IComService.IServiceBinder)service).getService(); serviceHandler.sendEmptyMessage(0); } @Override public void onServiceDisconnected(ComponentName arg0) { Log.i("INFO", "Service Unbound "); } };but the line
comService = ((IComService.IServiceBinder)service).getService();always throws a
05-02 22:12:55.922: ERROR/AndroidRuntime(622): java.lang.ClassCastException: android.os.BinderProxyI can't explain why, I followed the app sample on http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalServiceBinding.html
Any hints would be nice!
最满意答案
您需要使用AIDL来定义跨应用程序的接口(所谓的“远程服务”)。 您遵循本地绑定示例,但您没有使用本地绑定。 试试这个和这个示例项目的远程绑定示例使用AIDL。
You need to use AIDL to define interfaces that span applications (so-called "remote services"). You followed a local binding example, but you are not using local binding. Try this and this sample project for a remote binding example using AIDL.
更多推荐
发布评论