java– 使用服务时的ClassCastException

java– 使用服务时的ClassCastException,第1张

概述我定义了一个本地服务:publicclassComServiceextendsServiceimplementsIComService{privateIBinderbinder=newComServiceBinder();publicclassComServiceBinderextendsBinderimplementsIComService.IServiceBinder{publicIComServiceget

我定义了一个本地服务:

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(Component@R_403_6889@ @R_403_6889@, IBinder service) {        Log.i("INFO", "Service bound " + @R_403_6889@);        comService = ((IComService.IServiceBinder)service).getService();        serviceHandler.sendEmptyMessage(0);    }    @OverrIDe    public voID onServicedisconnected(Component@R_403_6889@ 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上关注了应用示例

任何提示都会很好!

解决方法:

您需要使用AIDL来定义跨应用程序的接口(所谓的“远程服务”).您遵循了本地绑定示例,但您没有使用本地绑定.使用AIDL尝试远程绑定示例的this和this示例项目.

总结

以上是内存溢出为你收集整理的java – 使用服务时的ClassCastException全部内容,希望文章能够帮你解决java – 使用服务时的ClassCastException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115301.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存