本地广播接收器异常:android.app.IntentReceiverLeaked?

时间:2018-10-08 07:38:01

标签: java android broadcastreceiver intentfilter

注意:带有android:excludeFromRecents="true"的活动标志

@Override
    protected void onDestroy() {

        sendBroadcast(new Intent("com.android.servicerestart"));
        if(broadcastReceiver != null) {
            try {
                unregisterReceiver(broadcastReceiver);
            }
            catch (IllegalArgumentException E)
            {

            }
        }

        super.onDestroy();
        Log.e("Activity","In OnDestroy");
    }

    @Override
    protected void onPause() {

        if(broadcastReceiver != null) {
            try {
                unregisterReceiver(broadcastReceiver);
            }
            catch (IllegalArgumentException E)
            {
                E.printStackTrace();
            }
        }
        super.onPause();
        Log.e("Activity","In OnPause");
    }

    @Override
    protected void onResume() {

        registerReceiver();
        super.onResume();
        Log.e("Activity","In onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        try {
            registerReceiver();
        }
        catch (Exception E)
        {

        }
    }

registerReceiver();

private void registerReceiver() {
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

        }
    };

    try {
        registerReceiver(broadcastReceiver, new IntentFilter("*******.receive.service.download"));
    }
    catch (Exception E)
    {

    }
}

日志

E/ActivityThread: Activity **********.activity.MainActivity has leaked IntentReceiver **********.activity.MainActivity$3@3318724 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity **********.activity.MainActivity has leaked IntentReceiver **********.activity.MainActivity$3@3318724 that was originally registered here. Are you missing a call to unregisterReceiver()?
    at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1164)
    at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:951)
    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1308)
    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1288)
    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1282)
    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:586)
    at **********.activity.MainActivity.registerReceiver(MainActivity.java:203)
    at **********.activity.MainActivity.onStart(MainActivity.java:156)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1255)
    at android.app.Activity.performStart(Activity.java:6853)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2712)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2810)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1530)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6256)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:793)

1 个答案:

答案 0 :(得分:1)

这是生命周期问题

startresume都注册了一个接收者-但是没有检查是否已经存在一个接收者(因此您注册了两个单独的接收者)

这意味着在pausedestroy方法上只能看到第二个方法,第一个方法被泄漏。

最好的解决方法是删除启动/销毁块。否则,您可以将new BroadcastReceiver块包装在if (broadcastReceiver == null)