Saturday, October 9, 2010

How to identify when lock screen is unlocked.?

My requirement was to listen for the intent when the screen is unlocked successfully.
Unfortunately, there is no such intent defined by Android framework, when the screen is unlocked successfully.

So i went into the framework code which takes care of showing lock screen, unlock screen and handling the events on that. It is present
/frameworks/policies/base/phone/com/android/internal/policy/impl

If in case you are working in framework level code and looking for the exact place in the code, here it is.

KeyguardViewMediator.java

Line no 801:
Here i have written code, to notify the Notification service, when the screen is unlocked. It works fine.



public void keyguardDone(boolean authenticated, boolean wakeup) {
synchronized (this) {
EventLog.writeEvent(70000, 2);
if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated + ")");
System.out.println("Keygaurd screen unlocked ");
mManager.notify("SCREEN_UNLOCKED"); // The code i added.
Message msg = mHandler.obtainMessage(KEYGUARD_DONE);
msg.arg1 = wakeup ? 1 : 0;
mHandler.sendMessage(msg);

if (authenticated) {
mUpdateMonitor.clearFailedAttempts();
}

if (mExitSecureCallback != null) {
mExitSecureCallback.onKeyguardExitResult(authenticated);
mExitSecureCallback = null;

if (authenticated) {
// after succesfully exiting securely, no need to reshow
// the keyguard when they've released the lock
mExternallyEnabled = true;
mNeedToReshowWhenReenabled = false;
}
}
}
}



There are intents for when the screen is ON & OFF, but you can use that for screen unlocking.

- ACTION_SCREEN_ON
- ACTION_SCREEN_OFF.

I found in one of the forums which suggesting in round other way.

- Wait for ACTION_SCREEN_ON.
- (After screen is on,) Wait for ACTION_MAIN with category CATEGORY_HOME (Which launches the home screen) - This is probably what is sent after the phone gets unlocked.

Not sure this works. !! Check it out.

No comments:

Post a Comment