BroadcastReceiver for screen lock not being triggered
Solution 1
Check out this tutorial Handling Screen off and On intents
Solution 2
For this problem, you have to create a infinite service, which is registering a local broadcast receiver for these intents. If you do this way, then your app will look for screen off but make sure you have to make service which will always running in background like reboot receiver
NickT
Updated on June 04, 2022Comments
-
NickT almost 2 years
I'm trying to turn the GPS location updates off when the screen locks. Having read the answer to this question Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?
I've written some code to implement a BroadcastReceiver but it's not working when the screen goes off.
I've registered a BroadcastReceiver in my code with
@Override public void onResume() { super.onResume(); IntentFilter iFilter = new IntentFilter(); iFilter.addAction("android.intent.action.ACTION_SCREEN_ON"); iFilter.addAction("android.intent.action.ACTION_SCREEN_OFF"); registerReceiver(screenStatReceiver, iFilter);
and the receiver itself is just a stub for now:
public BroadcastReceiver screenStatReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.d("BCAST_TAG", "Got broadcast"); String action = intent.getAction(); } };
and in the manifest I have:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Any ideas as to why it's not being triggered when I debug it on my phone?