我有一個簡單的Android應用程序,它包含一個broadcastreceiver類作爲MainActivity內部的類。代碼如下。BroadcastReceiver不工作在我的Android應用程序
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 [email protected]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 [email protected]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)
這裏提到的是關於registerReceiver()調用下面給出在MainActivity錯誤,
DataWriteReceiver dataWriteReceiver = new DataWriteReceiver();
this.registerReceiver(dataWriteReceiver,filter);
總結是MainActivity已泄漏IntentReceiver?我需要unregisterReceiver()?
請你幫我一把。
這裏的回答可以幫助你:http://stackoverflow.com/questions/9477922/android-broadcast-receiver-for-screen-on-and-screen-off – ifeegoo
並且:HTTP:/ /stackoverflow.com/questions/1588061/android-how-to-receive-broadcast-intents-action-screen-on-off – ifeegoo