2 ssummeraw ssummeraw 于 2013.07.12 11:37 提问

关闭 android 程序毁坏broadcast receiver

在 main activity 的 onCreate 方法中,我注册了 broadcastReciver。但是当我关闭程序时有下面的错误:

@429d47e0 that was originally registered here. Are you missing a call to unregisterReceiver()?
07-10 23:40:48.161: E/ActivityThread(20989):    at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
07-10 23:40:48.161: E/ActivityThread(20989):    at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
07-10 23:40:48.161: E/ActivityThread(20989):    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1274)
07-10 23:40:48.161: E/ActivityThread(20989):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1261)
07-10 23:40:48.161: E/ActivityThread(20989):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1255)
07-10 23:40:48.161: E/ActivityThread(20989):    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:372)

当关闭程序时我不想注销 receiver。我该怎么做呢?我以编程的方式注册的 receiver。

1个回答

cj6585256
cj6585256   2013.07.12 13:07
private YourReceiver receiver;

定义成成员变量

public void onDestory() {
if(receiver != null) {
unregisterBrodcastReceiver(receiver);
receiver = null;
}

}

这样就可以了,伪代码不要Copy

Csdn user default icon
上传中...
上传图片
插入图片