java.lang.IllegalArgumentException: Receiver not registered : Android BroadcastReceiver

     --------- beginning of crash

2021-06-29 14:58:18.780 16932-16932/com.example.myapplication2 E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.example.myapplication2, PID: 16932

    java.lang.IllegalArgumentException: Receiver not registered: com.example.myapplication2.Activities.Affirmation.ShowAffirmationActivity$broadcastReceiver$1@94b6d81

        at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:1433)

        at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1553)

        at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:674)

        at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:674)

        at com.example.myapplication2.Activities.Affirmation.ShowAffirmationActivity.unregisterReciever(ShowAffirmationActivity.kt:90)

        at com.example.myapplication2.Activities.Affirmation.ShowAffirmationActivity$broadcastReceiver$1.onReceive(ShowAffirmationActivity.kt:36)

        at androidx.localbroadcastmanager.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313)

        at androidx.localbroadcastmanager.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121)

        at android.os.Handler.dispatchMessage(Handler.java:107)

        at android.os.Looper.loop(Looper.java:224)

        at android.app.ActivityThread.main(ActivityThread.java:7562)

        at java.lang.reflect.Method.invoke(Native Method)

        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

2021-06-29 14:58:18.784 16932-16932/com.example.myapplication2 E/MQSEventManagerDelegate: failed to get MQSService.

2021-06-29 14:58:18.786 16932-16932/com.example.myapplication2 E/CustomActivityOnCrash: App has crashed, executing CustomActivityOnCrash's UncaughtExceptionHandler

    java.lang.IllegalArgumentException: Receiver not registered: com.example.myapplication2.Activities.Affirmation.ShowAffirmationActivity$broadcastReceiver$1@94b6d81

        at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:1433)

        at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1553)

        at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:674)

        at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:674)

        at com.example.myapplication2.Activities.Affirmation.ShowAffirmationActivity.unregisterReciever(ShowAffirmationActivity.kt:90)

        at com.example.myapplication2.Activities.Affirmation.ShowAffirmationActivity$broadcastReceiver$1.onReceive(ShowAffirmationActivity.kt:36)

        at androidx.localbroadcastmanager.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313)

        at androidx.localbroadcastmanager.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121)

        at android.os.Handler.dispatchMessage(Handler.java:107)

        at android.os.Looper.loop(Looper.java:224)

        at android.app.ActivityThread.main(ActivityThread.java:7562)

        at java.lang.reflect.Method.invoke(Native Method)

        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)


Explanation:

I am using directly unregisterReceiver(broadcastReceiver).

It's throwing error java.lang.IllegalArgumentException: Receiver not registered. You can check error in detail above.


Solution:

Use below to unregisterReceiver your local broadcast receiver.

LocalBroadcastManager.getInstance(this@ShowAffirmationActivity).unregisterReceiver(broadcastReceiver)

I have registered broad cast receiver in onCreate using below code.
LocalBroadcastManager.getInstance(this@ShowAffirmationActivity).registerReceiver(broadcastReceiver, IntentFilter(ACTION_FINISH_ALARM_ACTIVITY))

I have declared broad cast receiver at start of activity as shown below. This code only receives broad cast once and unregister broadcast receiver before finishing activity. This works perfectly fine. 
private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.d("ACTION_FINISH_ALARM::", "Status::"+intent.action)
if (ACTION_FINISH_ALARM_ACTIVITY.equals(intent.action)) // 1 == lock
{
unregisterReceiver()
alarmOptions?.stopVibrationAndRinging()
finishAndRemoveTask()
}
}
}

No comments:

Post a Comment

Popular Posts