我有一个简单的Android应用程序,它包含一个broadcastreceiver类作为MainActivity中的内部类。代码如下。
private class MyReceiver extends BroadcastReceiver {
private Intent receivedIntent;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("MyTag","onReceive function....!!!!!");
receivedIntent = intent;
}
}
根据一些标准教程,我可以在清单文件中为接收者提供静态条目。
<receiver
android:name = ".MyReceiver"
android:enabled = "true">
<intent-filter>
<action android:name = "android.intent.action.ACTION_SCREEN_OFF"/>
<action android:name = "android.intent.action.ACTION_SCREEN_ON"/>
</intent-filter>
</receiver>
但是直到我在MainActivity中添加以下行才会有效。
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
DataWriteReceiver dataWriteReceiver = new DataWriteReceiver();
this.registerReceiver(dataWriteReceiver,filter);
即使在清单文件中输入后,我还需要执行registerReceiver吗?任何帮助表示赞赏。
但是当关闭我的应用时,我遇到了另一个问题。 这就是ADM显示的错误。
11-02 23:01:10.178: E/ActivityThread(11121): Activity com.sony.datamoduledesignproject.MainActivity has leaked IntentReceiver com.sony.datamoduledesignproject.MainActivity$DataWriteReceiver@2911ab22 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-02 23:01:10.178: E/ActivityThread(11121): android.app.IntentReceiverLeaked: Activity com.sony.datamoduledesignproject.MainActivity has leaked IntentReceiver com.sony.datamoduledesignproject.MainActivity$DataWriteReceiver@2911ab22 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:970)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:771)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:2014)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1994)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1988)
11-02 23:01:10.178: E/ActivityThread(11121): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:503)
11-02 23:01:10.178: E/ActivityThread(11121): at com.sony.datamoduledesignproject.MainActivity.intentGenerator(MainActivity.java:93)
11-02 23:01:10.178: E/ActivityThread(11121): at com.sony.datamoduledesignproject.MainActivity.onCreate(MainActivity.java:78)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.Activity.performCreate(Activity.java:6374)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ActivityThread.access$900(ActivityThread.java:181)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
11-02 23:01:10.178: E/ActivityThread(11121): at android.os.Handler.dispatchMessage(Handler.java:102)
11-02 23:01:10.178: E/ActivityThread(11121): at android.os.Looper.loop(Looper.java:145)
11-02 23:01:10.178: E/ActivityThread(11121): at android.app.ActivityThread.main(ActivityThread.java:6145)
11-02 23:01:10.178: E/ActivityThread(11121): at java.lang.reflect.Method.invoke(Native Method)
11-02 23:01:10.178: E/ActivityThread(11121): at java.lang.reflect.Method.invoke(Method.java:372)
11-02 23:01:10.178: E/ActivityThread(11121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
11-02 23:01:10.178: E/ActivityThread(11121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
这里提到的MainActivity中的错误在registerReceiver()调用上,如下所示,
DataWriteReceiver dataWriteReceiver = new DataWriteReceiver();
this.registerReceiver(dataWriteReceiver,filter);
总结是MainActivity泄露了IntentReceiver,我是否需要取消注册Receiver()?
请你帮我解决。
答案 0 :(得分:5)
人们多次提出这样的问题,我刚才搜索过这类问题,但没有一个答案向我们展示了关于此的官方答案。所以这里
http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_ON
http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF
请记住:Android官方文档可以让您更好地了解Android,有时不会,但每次都是ALMOST。