1

我在动态注册 BroacasrReceiver 时遇到问题CallReceiver。当触发onReceive方法时,CallReceiver会抛出致命异常。

...
private boolean isCRRegistered = false;

public int onStartCommand (Intent intent, int flags, int startId) {

     if(!isCRRegistered)    {
     IntentFilter filter2 = new IntentFilter("android.intent.action.PHONE_STATE");
     CallReceiver mCallReceiver = new CallReceiver();
     registerReceiver(mCallReceiver, filter2);

     //registering some listener
     mCallReceiver.registerListener(this);
     }
}

注销:

if(isCRRegistered)  {
        unregisterReceiver (mCallReceiver);
}

日志猫:

02-04 14:41:50.286: E/AndroidRuntime(28328): FATAL EXCEPTION: main
02-04 14:41:50.286: E/AndroidRuntime(28328): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.PHONE_STATE flg=0x20000000 (has extras) } in com.kris.intellignetringer.CallReceiver@448162b8
02-04 14:41:50.286: E/AndroidRuntime(28328):    at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:934)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at android.os.Handler.handleCallback(Handler.java:587)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at android.os.Looper.loop(Looper.java:143)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at android.app.ActivityThread.main(ActivityThread.java:4914)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at java.lang.reflect.Method.invoke(Method.java:521)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at dalvik.system.NativeStart.main(Native Method)
02-04 14:41:50.286: E/AndroidRuntime(28328): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.kris.intellignetringer.ScreenReceiver@44816d60
02-04 14:41:50.286: E/AndroidRuntime(28328):    at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:812)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:822)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:331)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at com.kris.intellignetringer.StateChecker.serviceDestroy(StateChecker.java:126)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at com.kris.intellignetringer.StateChecker.onPhoneStateChange(StateChecker.java:36)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at com.kris.intellignetringer.CallReceiver.onReceive(CallReceiver.java:50)
02-04 14:41:50.286: E/AndroidRuntime(28328):    at  android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:917)
02-04 14:41:50.286: E/AndroidRuntime(28328):    ... 9 more
4

1 回答 1

6

错误不在这里!错误是当您取消注册接收器时。看来您正在有条件地注册接收器。

因此,在注销接收器之前,请确保您已经真正注册了它。

您可以使用注册接收器后设置的布尔值。然后,当您想注销接收器时,请确保设置了布尔值,否则(如果不是(false))您不会注销。

于 2013-02-04T14:17:08.057 回答