user456
user456

Reputation: 262

Starting android notification settings activity causes Leaked intent receiver

In my app I am using below code to display notification settings to the user.

Below is the code.

    private void startNotificationSettings(){
    final Activity activity =  getActivity();
    Intent intent = new Intent();
    intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");

    if(UIUtils.isAVersion(8)){
        intent.putExtra("android.provider.extra.APP_PACKAGE", activity.getPackageName());

    }else if(UIUtils.isAfterVersion(Build.VERSION_CODES.LOLLIPOP )){
        intent.putExtra("app_package", activity.getPackageName());
        intent.putExtra("app_uid", activity.getApplicationInfo().uid);
    }else{
        Util.alert(activity,"Warning!","Operation not supported for your OS version",Util.ALERT_NO_ACTION);
        return;
    }
    startActivity(intent);
}

It works no problem but when i press on back button it throws the error below.

Activity com.android.settings.Settings$AppNotificationSettingsActivity has leaked IntentReceiver com.android.settings.accounts.AuthenticatorHelper@cb5c59f that was originally registered here. Are you missing a call to unregisterReceiver()?
                                               android.app.IntentReceiverLeaked: Activity com.android.settings.Settings$AppNotificationSettingsActivity has leaked IntentReceiver com.android.settings.accounts.AuthenticatorHelper@cb5c59f that was originally registered here. Are you missing a call to unregisterReceiver()?
                                                   at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1030)
                                                   at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:817)
                                                   at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1256)
                                                   at android.app.ContextImpl.registerReceiverAsUser(ContextImpl.java:1243)
                                                   at android.content.ContextWrapper.registerReceiverAsUser(ContextWrapper.java:595)
                                                   at com.android.settings.accounts.AuthenticatorHelper.listenToAccountUpdates(AuthenticatorHelper.java:264)
                                                   at com.android.settings.SettingsActivity.onCreate(SettingsActivity.java:1706)
                                                   at android.app.Activity.performCreate(Activity.java:6876)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                                                   at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:158)
                                                   at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Any idea how not to leak this?

Thanks

Upvotes: 1

Views: 464

Answers (1)

Ali Asadi
Ali Asadi

Reputation: 987

The problem is that you are not calling unregisterReceiver for the BroadcastReceiver that you registered with registerReceiver on your AppNotificationSettingsActivity

Un-register your receivers in onStop in your activity:

@Override
protected void onStop(){
    unregisterReceiver(your_receiver);
    super.onStop();
}

Upvotes: 1

Related Questions