Reputation: 33
I have a simple Android App which contains a broadcastreceiver class as an inner class inside the MainActivity. The code is given below.
private class MyReceiver extends BroadcastReceiver {
private Intent receivedIntent;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("MyTag","onReceive function....!!!!!");
receivedIntent = intent;
}
}
According to some standard tutorials, I can have a static entry for the receiver inside the manifest file.
<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>
But it is not working until I put the following lines in MainActivity.
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
DataWriteReceiver dataWriteReceiver = new DataWriteReceiver();
this.registerReceiver(dataWriteReceiver,filter);
Do I need to do registerReceiver even after the entry in the manifest file? Any help is appreciated.
But there is another problem that I faced while closing my app. This is what ADM shows as an error.
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)
The error in MainActivity as mentioned here is on the registerReceiver() call as given below,
DataWriteReceiver dataWriteReceiver = new DataWriteReceiver();
this.registerReceiver(dataWriteReceiver,filter);
Summary is that MainActivity has leaked IntentReceiver and do I need to unregisterReceiver()?
Please can you help me out.
Upvotes: 3
Views: 732
Reputation: 7292
People asked this kind of question many times,I have searched just now for this kind of question,but none of the answers show us the official answer about this.So here
http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_ON
http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF
Remember:The Android official documentation will give you better basic understanding of the Android,not sometimes,but ALMOST everytime.
Upvotes: 5