当前位置: 首页 > news >正文

java.lang.ClassCastException: android.os.BinderProxy cannot be cast to ...

        项目开发遇到下面这个报错了!

问题原因

        直接说原因,就是因为进程间不能直接传递对象,应该传递该Binder对象的映射(代理对象),所以类型转换就出错了。如果在同一个进程中,直接传递对象没有关系,下面的类型强转是没有问题的,但是如果是不同进程,强转就会失败,就会报上面的错误。

        那启动应用内启动service怎么就成进程间通信了呢?因为service是指定了android:process=":remote"。就算自己的service没有指定android:process,如果应用中其他service写了android:process,也会导致自己的service报错。

        我的Service是一个websocket通信服务,本来我把bindService写在Activity中,但是这有一个问题,就是页面跳转,服务会随着声明周期被销毁重建,这样每次跳转页面都重新连接,浪费时间,于是我就把Service挪到了Application里面。我的页面一个LoginActivity,一个MainActivity,页面跳转到MainActivity的时候就报错了,因为我的MainActivity里面使用了百度地图,而百度地图的service如下

<!--        百度地图定位的service组件--><service android:name="com.baidu.location.f"android:enabled="true"android:process=":remote"/>

这样就导致我的Service的binder转换报错了。把百度地图Service里面的android:process=":remote"去掉就就可以了。

解决办法

一.去掉android:process=":remote"
二.aidl方式

        下面记录一下,进程间Service要想解决这个问题,就是需要用aidl方式。

1.创建aidl文件

        Android Studio可以直接创建,在与java文件平级的文件下会创建一个aidl文件夹,里面包名是app的包名,创建一个后缀为.aidl的文件夹

创建之后是这样的

aidl文件如下:

// IMyAidlInterface.aidl
package com.deviser.androidremote;// Declare any non-default types here with import statementsinterface IMyAidlInterface {//这用来传递Service的包名和文件名,传递之后通过反射获取Service对象String getName();
}

编译之后,build下面会生成

2.重写Service的Binder方法
//    //用于Activity和service通讯//这是原来的方法,直接把WebClientService这个对象返回了
//    public class WebClientBinder extends Binder {
//        public WebClientService getService() {
//            return WebClientService.this;
//        }
//    }//通过aidl方式,继承IMyAidlInterface.Stub,返回的是WebClientService的包名和文件名//进程间传递的是字符串public class WebClientBinder extends IMyAidlInterface.Stub{@Overridepublic String getName() throws RemoteException {return WebClientService.class.getName();}}
3.修改onServiceConnected
    private var webServiceConnection: ServiceConnection = object : ServiceConnection {override fun onServiceConnected(componentName: ComponentName?, iBinder: IBinder?) {//原来就是这里iBinder强转成WebClientService.WebClientBinder报错
//            var binder: WebClientService.WebClientBinder = iBinder as WebClientService.WebClientBinder
//            webClientService = binder.service// 获得代理对象var proxy = IMyAidlInterface.Stub.asInterface(iBinder)try {//通过反射机制,把字符串转换成对象WebClientService对象var clazz = Class.forName(proxy.name)webClientService = clazz.newInstance() as WebClientService?}catch (e: Exception){e.printStackTrace()}}override fun onServiceDisconnected(componentName: ComponentName?) {Log.e(MyConstant.TAG,"服务与活动成功断开");}}

到这里,问题就解决啦!

http://www.lryc.cn/news/164891.html

相关文章:

  • AIGC(生成式AI)试用 3 -- 专业主题
  • rsyslog-日志管理 logrotate-日志轮转
  • 类和对象续
  • SpringCloud:Feign实现微服务之间相互请求
  • LeetCode 1359. Count All Valid Pickup and Delivery Options【动态规划,组合数学】1722
  • [杂谈]-从硬件角度理解二进制数
  • Fast-DDS 服务发现简要概述
  • 基于spingboot的websocket订阅、广播、多人聊天室示例
  • Linux mac Windows三系统 局域网文件共享方法
  • Java——比较器
  • 【数据结构】初识泛型
  • 代码随想录--哈希--有效的字母异位词
  • MySQL——数据的增删改
  • 云服务器与http服务器
  • golang教程 beego框架笔记一
  • 【深度学习】Mini-Batch梯度下降法
  • AI项目六:WEB端部署YOLOv5
  • 敲代码常用快捷键
  • MyBatis: 分页插件PageHelper直接传递分页参数的用法
  • Python基于Flask的高校舆情分析,舆情监控可视化系统
  • Python第一次作业练习
  • InstallShield打包升级时不覆盖原有文件的解决方案
  • 服务器巡检表-监控指标
  • 无涯教程-JavaScript - DDB函数
  • uniapp打包微信小程序。报错:https://api.weixin.qq.com 不在以下 request 合法域名列表
  • stm32之31.iic
  • 新的 ChatGPT 提示工程技术:程序模拟
  • 【Python】爬虫基础
  • leetcode分类刷题:队列(Queue)(三、优先队列用于归并排序)
  • 无线窨井水位监测仪|排水管网智慧窨井液位计安装案例