使用服务时出现ClassCastException(ClassCastException while using service)

编程入门 行业动态 更新时间:2024-10-27 14:26:06
使用服务时出现ClassCastException(ClassCastException while using 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; } }

相应的界面:

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.BinderProxy

I 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.

更多推荐

本文发布于:2023-07-22 12:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ClassCastException   service

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!