java.lang.IllegalArgumentException

  • Exception: Receiver not registered

  • Occurrence: occasionally

  • Cause: Broadcast calls unregistered broadcast without registering.

  • Problem scenario: Usually, most dynamic broadcast registration is written in the onCreate method of the Activity, and the logout is written in the onDestory, so as to ensure that when calling the broadcast logout method, the broadcast must be registered, but in other places, may be due to the code logic is not strict enough, and when the logout occurs, The broadcast was not registered.

  • Problem derivation:

    1. An exception will be reported if multiple calls to unregister a broadcast are made:

       "Unregistering Receiver " + r+ " that was already unregistered"
      Copy the code
  • Source location:

    1. File path: android app. ReceiverRestrictedContext. Java
     @Override
     public void unregisterReceiver(BroadcastReceiver receiver) {
         if(mPackageInfo ! =null) {
             IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
                     getOuterContext(), receiver);
             try {
                 ActivityManager.getService().unregisterReceiver(rd);
             } catch (RemoteException e) {
                 throwe.rethrowFromSystemServer(); }}else {
             throw new RuntimeException("Not supported in system context");
         }
    
     public IIntentReceiver forgetReceiverDispatcher(Context context, BroadcastReceiver r) {...if (context == null) {
                 throw new IllegalStateException("Unbinding Receiver " + r
                         + " from Context that is no longer in use: " + context);
             } else {
                 throw new IllegalArgumentException("Receiver not registered: " + r);
             }
         }
     } ```Copy the code





Writer: Running crew head brother


Link: https://www.jianshu.com/p/e98735bda717


Source: Jane Book


Brief book copyright belongs to the author, any form of reprint please contact the author to obtain authorization and indicate the source.