unregisterReceiver报错处理 android

本文介绍了在Android开发中如何安全地注销广播接收器,避免因重复注销而导致的应用崩溃。通过设置标记变量和使用try-catch结构来处理可能发生的异常。

当注册广播时,常会遇到的问题就是重复注销广播处理函数是会报错,而且会让进程奔溃。

一般来说,可以通过一个变量来保存广播处理是否被注销,每当注销时,将它标记为false。如果再次注销时遇到false就不对他进行注销处理。

但是程序比较复杂的时候会比较难控制。可以使用try catch方式捕获错误。

try{
    unregisterReceiver(receiver);  
} 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;  
    }  
}  

转载自:http://blog.youkuaiyun.com/hzbigdog/article/details/11062961

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值