0

Well some months ago I learned the basics of android and now I'm triying to practice to remember what I learned. So the problem is that I'm doing an app that when it catches a change in the status of the screen (screen on/screen off) it does something. I want that when the app is not running (becausethe user killed it by pressing the home button or something like that) it still does what I want. I have decided to use receiver but I don't know if it's the correct option.

If the app is minimized it works but the problem whenthe user presses the "recent apps" button and slides the app. Then the receiver doesn't catch anything.

In the manifest I've declared:

<receiver android:name=".MyReceiver" android:enabled="true">
    <intent-filter>
       <action android:name="android.intent.action.SCREEN_ON"/>
       <action android:name="android.intent.action.SCREEN_OFF"/>
    </intent-filter>
</receiver>

My main activity (maybe I have something wrong there):

public class MainActivity extends Activity {

    private MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, filter);
    }


   @Override
   protected void onDestroy() {
       if (myReceiver != null) {
          unregisterReceiver(myReceiver);
          myReceiver = null;
       }
       super.onDestroy();

   }
}

and my receiver:

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
       String action = intent.getAction();
       if (action.equals("android.intent.action.SCREEN_OFF")) {
          Log.e("In on receive", "In Method: ACTION_SCREEN_OFF");
          Toast.makeText(context, "DO SOMETHING",Toast.LENGTH_LONG).show();
       }
       else if (action.equals("android.intent.action.SCREEN_ON")) {
          Log.e("In on receive", "In Method: ACTION_SCREEN_ON");
          Toast.makeText(context, "DO SOMETHING2",Toast.LENGTH_LONG).show();
       }
    }
}

Really appreciate if you could take a look :D.
Thank you

xampla
  • 157
  • 1
  • 1
  • 10

1 Answers1

1

You have registered the receiver in the manifest. So don't register and unregister it in the MainActivity. That's the problem. So once the app is killed, onDestroy() gets called and your receiver is unregistered and will no longer listen.

Declaring the the receiver in the manifest means that your app will always listen to broadcasts. And that's exactly what you want. So remove the register/unregister part from the MainActivity.

UPDATE: It seems that SCREEN_ON and SCREEN_OFF can't be registered via the manifest. This might possibly be for a security reason. So in this case you have to register this via code. But the problem here is that, once you quit the app, onDestroy() is called and you are no longer listening. If you are app really need this feature, you have to create a service and have that run constantly in the the background. You can use that to listen to the broadcast.

Henry
  • 17,490
  • 7
  • 63
  • 98
  • So in the __onDestroy()__ I have to eliminate the "if" clause and in the __onCreate()__ I have to eliminate just the lines where I create __myReceiver__ and __registerReceiver(myReceiver, filter)__ If I've understood it right, isn't it? – xampla Oct 21 '15 at 17:59
  • Yes. You can register broadcast receivers via the manifest or via java code. Since you have already registered it in the manifest, there is no longer a need to do it in the code too. – Henry Oct 21 '15 at 18:08
  • Unfortunately it's still not working. In fact now it doesn't work even if the app is working. Any idea? – xampla Oct 21 '15 at 18:15
  • Interesting. This means that your receiver that you registered via manifest never worked in the first place and only the register via code was working The problem is in the way it's registered in the manifest. Could you remove one action from the intent-filter and try. – Henry Oct 21 '15 at 18:18
  • I've removed the `` and nothing I don't see the logs and it doesn't show the toast :( – xampla Oct 21 '15 at 18:22
  • Updated my answer. Manifest won't work out for you in this case, you got to register/unregister via code. – Henry Oct 21 '15 at 18:29
  • Ahh I see. So do I just have to implement my receiver in a service? – xampla Oct 21 '15 at 18:38