After struggling with this for a while, I've found that the best way to do this is to register a BroadcastReceiver on the "android.intent.action.USER_PRESENT" action.
"Broadcast Action: Sent when the user is present after device wakes up (e.g when the key-guard is gone)."
To distinguish between cases where the user has turned on phone screen when it wasn't locked, and when they actually unlocked it 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()) {
//phone was unlocked, do stuff here
}
}
}