Archive

Archive for February, 2011

Screen ON And Unlock keypad

February 3, 2011 Leave a comment

Screen ON: Required when activity is in foreground and performing long running task
Use below code snippet And add "android.permission.WAKE_LOCK" in app manifest

PowerManager powermgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakelock = powermgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wakelock.acquire();
// call long running task
...
..
wakelock.release();

Unlock keypad: Required when using intents. example: Making a phone call with user activity and after completing phone call user acitivity shall not be held with a keylock

Use below code snippet inside Onresume() method of the activity. And add "android.permission.DISABLE_KEYGUARD" in app manifest
public void checkKeypadLock()
{
KeyguardManager Keylockmgr = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
Keylockmgr.newKeyguardLock("My Tag").disableKeyguard();
}


Complete Usage of Disabling and Reenabling keyguard on Android

if (mKeyguardManager == null) {
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock1 = mKeyguardManager.newKeyguardLock("QApp");
mKeyguardEnabled = true;
}

PowerManager powermgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakelock = powermgr.newWakeLock(PowerManager.FULL_WAKE_LOCK| PowerManager.ON_AFTER_RELEASE| PowerManager.ACQUIRE_CAUSES_WAKEUP,"QApp");

wakelock.acquire();

if (mKeyguardEnabled) {
mKeyguardLock1.disableKeyguard();//

if(mProductID == 1)
retval = mCalculator.Calc(mcalcJob,isOnline);
else if(mProductID == 2)
retval = m_pCalculator.Calc(mcalcJob,isOnline);

mprogressDialog.dismiss();
mHandler.post(updateResults);

// ON sony ericsson the keylock is very quick, so we wait for 3Sec before reenabling keyguard only on Xperia 10i
if(Build.MANUFACTURER.startsWith("Sony"))
{
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
wakelock.release();
mKeyguardLock1.reenableKeyguard();

Categories: Android Dev