gpt4 book ai didi

android - 小部件。 Activity 泄露了 IntentReceiver

转载 作者:行者123 更新时间:2023-11-29 21:56:25 26 4
gpt4 key购买 nike

在我的 Widget 中,我使用:

Intent intent = new Intent(context, ClassForWidget.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget1);
views.setOnClickPendingIntent(R.id.layout_widget1, pendingIntent);`

我的类 ClassForWidget 已经有一个 BroadcastReceiver:

BroadcastReceiver myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);

所以,当我点击 Widget 时,出现错误:

Activity .ClassForWidget has leaked IntentReceiver .ClassForWidget$MyReceiver@40ebe710 that was originally registered here. Are you missing a call to unregisterReceiver()?

我该如何解决?谢谢。

最佳答案

通常你应该在onResume注册,在onPause(或onStart和onStop)取消注册。

protected void onPause() {
if (myReceiver != null){
unregisterReceiver(myReceiver);
myReceiver = null;
}

}

希望对你有帮助。

关于android - 小部件。 Activity 泄露了 IntentReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13119157/

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