android 的unregisterReceiver报错处理 提示“Receiver not registered”

本文介绍了解决Android应用中广播注销时遇到的Receivernotregistered异常的方法,通过自定义逻辑判断广播注册状态,确保程序稳定运行。

使用广播时,有时注销广播会出现Receiver not registered的异常,导致程序崩溃。

出现的原因有可能五花八门,但是系统并没有提供方法让我们在注销广播时判断该广播是否注册。所以我们只能另辟蹊径的解决该问题了。

我的解决办法是:

if (homePressReceiver != null) {
try{
unregisterReceiver(homePressReceiver);
}catch(IllegalArgumentException e){
if (e.getMessage().contains("Receiver not registered")) {  
       // Ignore this exception. This is exactly what is desired  
   } else {  
       // unexpected, re-throw  
       throw e;  
   } 
}

}


当然,也有可能是多重调用注销广播,所以写代码时要注意registerReceiver和 unregisterReceiver要成对出现!

03-16 12:33:00.942 18324 18324 E AndroidRuntime: FATAL EXCEPTION: main 03-16 12:33:00.942 18324 18324 E AndroidRuntime: Process: com.android.bluetooth, PID: 18324 03-16 12:33:00.942 18324 18324 E AndroidRuntime: java.lang.IllegalArgumentException: Receiver not registered: com.oplus.bluetooth.feature.dcs.OplusBluetoothRecorderFactory$1@f666353 03-16 12:33:00.942 18324 18324 E AndroidRuntime: at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:1777) 03-16 12:33:00.942 18324 18324 E AndroidRuntime: at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1922) 03-16 12:33:00.942 18324 18324 E AndroidRuntime: at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:832) 03-16 12:33:00.942 18324 18324 E AndroidRuntime: at com.oplus.bluetooth.feature.dcs.03-16 12:33:00.942 18324 18324 E AndroidRuntime: FATAL EXCEPTION: main 03-16 12:33:00.942 18324 18324 E AndroidRuntime: Process: com.android.bluetooth, PID: 18324 03-16 12:33:00.942 18324 18324 E AndroidRuntime: java.lang.IllegalArgumentException: Receiver not registered: com.oplus.bluetooth.feature.dcs.OplusBluetoothRecorderFactory$1@f666353 03-16 12:33:00.942 18324 18324 E AndroidRuntime: at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:1777) 03-16 12:33:00.942 18324 18324 E AndroidRuntime: at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1922) 03-16 12:33:00.942 18324 18324 E AndroidRuntime: at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:832) 03-16 12:33:00.942 18324 18324 E AndroidRuntime: at com.oplus.bluetooth.feature.dcs.OplusBluetoothRecorderFactory.cleanUp(OplusBluetoothRecorderFactory.java:312).cleanUp(OplusBluetoothRecorderFactory.java:312) 帮我看看报错是什么,是什么类型的报错,怎么导致的
03-21
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值