gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:53:01 27 4
gpt4 key购买 nike

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

@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)

最佳答案

这是一个生命周期问题

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

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

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

关于java - 本地广播接收器异常 : android. app.IntentReceiverLeaked?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52697490/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com