How can I tell if the current Activity's OnResume has fired because the Application is resuming after the screen was unlocked rather than simply because it was temporarily in the background?
Asked
Active
Viewed 1,464 times
2 Answers
2
You can use a BroadcastReceiver
to listen to the screen unlock Intent
:
Implement this BroadcastReceiver
:
public class UnlockBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
final String action = intent.getAction();
if(Intent.ACTION_USER_PRESENT.equals(action)){
// Screen unlocked (Keyguard is gone)
}
}
}
And then you can register and unregister the BroadcastReceiver
in onResume()
and onPause()
like this:
private final UnlockBroadcastReceiver unlockReceiver = new UnlockBroadcastReceiver();
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
registerReceiver(unlockReceiver, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(unlockReceiver);
}
You can alternatively also register it in the manifest, but that does NOT work most of the time.
<receiver android:name="path.to.UnlockBroadcastReceiver">
<intent-filter android:enabled="true" android:exported="false">
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>

Xaver Kapeller
- 49,491
- 11
- 98
- 86
-
I did indeed need to a BroadcastReceiver and USER_PRESENT action but the only way to get it to fire is to register the receiver in code. Xaver if you update your answer to refer to this answer (http://stackoverflow.com/a/1588267/63286) around code registration I will then mark yours as MY answer – Pat Long - Munkii Yebee Jul 21 '14 at 17:06
-
I updated my answer. Could you test if it is working with the changes I made to the manifest? I am currently not in front of my computer and cannot test it myself. – Xaver Kapeller Jul 21 '14 at 17:12
-
I had already tried those two attributes. Did you see what @CommonsWare said? – Pat Long - Munkii Yebee Jul 21 '14 at 17:13
-
Yes I saw, it seems such a `BroadcastReceiver` can only be registered in code. But I will test this more thoroughly when I find the time. – Xaver Kapeller Jul 21 '14 at 17:15
-
Xaver did you "test thoroughly"? I want to mark your answer as "the one" but it needs to have the code regsitration in it IMHO – Pat Long - Munkii Yebee Jul 31 '14 at 09:00
-
Yes I tested it, and I found out that registering the `BroadcastReceiver` in the manifest actually works on some devices with specific Android versions. But I have still no idea why it works on those devices and not on the others. – Xaver Kapeller Jul 31 '14 at 09:06
0
In Activity or Service:
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
BroadcastReceiver mReceiver = new receiverScreen();
registerReceiver(mReceiver, filter);
} catch (Exception e) {
}
receiver code where System inform you if Screen on/off happen:
public class receiverScreen extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
}
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
}
}
}
If it IS locked, you can just write to a SharedPreference. Then on your activity's onResume(), you can check for the value of the sharedPreference
In your manifest.xml
<receiver android:name="receiverScreen">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.Intent.action.USER_PRESENT" />
</intent-filter>
</receiver>

harveyslash
- 5,906
- 12
- 58
- 111
-
1That is a bad solution. It won't actually work as the OP wants to. – Xaver Kapeller Jul 21 '14 at 12:54
-
1@harvey_slash - Please note the action should be - `android.intent.action.USER_PRESENT` in manifest file. The action `"android.Intent.ACTION_USER_PRESENT"` is incorrect. – sjain Jul 21 '14 at 12:59