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!
android android-wake-lock keyguard
Paul Richter Feb 07 '13 at 0:37 2013-02-07 00:37
source share