After some time debugging, I found that the best way to do this is to register the BroadcastReceiver in the action "android.intent.action.USER_PRESENT".
"Broadcast Action: Dispatched when a user is present after waking the device (for example, when the keyboard is gone)."
To distinguish between cases when the user turned on his screen, when he was not locked, when he really unlocked, use the KeyguardManager to check the security settings.
Code example:
Add this to your activity:
registerReceiver(new PhoneUnlockedReceiver(), new IntentFilter("android.intent.action.USER_PRESENT"));
Then use this class:
public class PhoneUnlockedReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { KeyguardManager keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE); if (keyguardManager.isKeyguardSecure()) {
Doron Manor Feb 24 '15 at 11:45 2015-02-24 11:45
source share