Android - Wake Up and Unlock Device

Description of the application: The application is intended as a security program for a specific client (not be published publicly). If the application has not detected movement within a certain period of time, the application should give an alarm and come to the forefront if it is in the background or the device is sleeping.

Problem: If the device is sleeping and locked, we need to wake up and unlock the device. Using various methods found here in SO and other places, we were able to (partially) wake up and unlock the device, however this ONLY behaves correctly when the device is physically connected to the computer. If the device sits on its own, it turns off, and we check the unlock after waking up, nothing happens; the device seems to continue to sleep, and the application does not seem to do anything (without alarm).

I used this post about using PowerManager and KeyguardManager and this post using window flags.

Here is the code used to wake up the device:

public void wakeDevice() { PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG"); wakeLock.acquire(); KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG"); keyguardLock.disableKeyguard(); runOnUiThread(new Runnable(){ public void run(){ getWindow().addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); } }); } 

From the comments and posts about some other SO issues I've seen / used, it seems that the PowerManager / KeyguardManager code should have done the trick. Again, as I said, it technically works when the device connects via USB to the dev machine, but does nothing while the device is split.

Also note that this is our first Android app, and therefore we are fully aware that we can completely abandon what we are trying to do. Any suggestions are welcome.

In short, given the code above, why does the device behave so differently based on whether it is connected and what we need to change to wake and unlock the device as described? Thank you in advance for your help!

+42
android android-wake-lock keyguard
Feb 07 '13 at 0:37
source share
1 answer

I solved the problem. The reason we observed different behavior when connecting the device via USB was because the device’s processor did not sleep. I assume that this is either the result of setting up the debug mode, or simply how it behaves when connected to a computer, since the energy-saving function of the processor’s sleep mode does not matter. Obviously, when the device is not connected, the processor will happily take a nap, and although we observe how the application works randomly (it would wake up at any time), the time would be inconsistent. I also assume that this is due to the fact that several processor cycles that have occurred are allocated sparingly, and very few cycles will be given to our application in "random" times.

So, our solution was to capture a partial follow lock when the device goes into the background (which runs in the onPause method), and release the lock in the onResume method. This seems to prevent the processor from sleeping. We continue to use full tracking lock and key lock to wake the device when we need. Using partial trail blocking seems to keep the processor from sleeping, and the device seems to wake up normally when expected. Here is our updated code if anyone encounters this problem:

 // Called from onCreate protected void createWakeLocks(){ PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK"); partialWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK"); } // Called implicitly when device is about to sleep or application is backgrounded protected void onPause(){ super.onPause(); partialWakeLock.acquire(); } // Called implicitly when device is about to wake up or foregrounded protected void onResume(){ super.onResume(); if(fullWakeLock.isHeld()){ fullWakeLock.release(); } if(partialWakeLock.isHeld()){ partialWakeLock.release(); } } // Called whenever we need to wake up the device public void wakeDevice() { fullWakeLock.acquire(); KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG"); keyguardLock.disableKeyguard(); } 
+55
Feb 07 '13 at 17:24
source share



All Articles