2013-11-19

Android API to unlock device and turn the screen on.

When you doing a notification or some like incoming call application. You usually need to unlock device and turn the screen on to let user seeing your notification. What you need is to disable keyguard and make a wake lock.

Disable keyguard (Unlock device)
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock kl = km.newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
Also remember to add permission to your AndroidManifest.xml
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> 

Acquire wake lock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
mWakeLock.acquire();
Don't forget to release wake lock when your notification disabled.
mWakeLock.release();
The permission you need for wake lock:
<uses-permission android:name="android.permission.WAKE_LOCK" />

No comments: