3

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.

  • The answer here can help you: http://stackoverflow.com/questions/9477922/android-broadcast-receiver-for-screen-on-and-screen-off – ifeegoo Nov 02 '15 at 13:38
  • And also :http://stackoverflow.com/questions/1588061/android-how-to-receive-broadcast-intents-action-screen-on-off – ifeegoo Nov 02 '15 at 13:47

1 Answers1

5

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

enter image description here

http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF

enter image description here

Remember:The Android official documentation will give you better basic understanding of the Android,not sometimes,but ALMOST everytime.

ifeegoo
  • 7,054
  • 3
  • 33
  • 38
  • Thank you! It's OK right now for me,but maybe I will find the Android official will not tell me something in the future. :-) – ifeegoo Nov 02 '15 at 14:06
  • The last statement is just for me to remember the importance of the reading of Android official documentation,yeah,it's not 100 percent. :-) :-) :-) – ifeegoo Nov 02 '15 at 14:09
  • 1
    that's right, if should read: `"... not sometimes,but ALMOST everytime"` but of course reading the official docs is a must and much people dont care about that... – pskink Nov 02 '15 at 14:11
  • I have adopted your sentence!Yes,I quite agree with you and I have met lots of people who don't care about the official docs. – ifeegoo Nov 02 '15 at 14:14
  • Thank you. I did exactly that. Actions are now recognized. – Suman Bandopadhyay Nov 02 '15 at 17:33
  • That's all right.Almost everytime if you want to make a better basic understanding of the Android,you should read the official Android documentation.Good Luck. – ifeegoo Nov 03 '15 at 01:28