android 的unregisterReceiver报错处理

本文介绍如何在复杂的程序中通过try-catch结构避免广播注销导致的异常情况,确保应用稳定运行。


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

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

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


[java] view plain copy
  1. try {  
  2.     unregisterReceiver(receiver);  
  3. catch (IllegalArgumentException e) {  
  4.     if (e.getMessage().contains("Receiver not registered")) {  
  5.         // Ignore this exception. This is exactly what is desired  
  6.     } else {  
  7.         // unexpected, re-throw  
  8.         throw e;  
  9.     }  
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、付费专栏及课程。

余额充值