I'm just wondering if I do this correctly; I'm programming a notification app which can display a notification when the phone is sleeping
Disable keyguard lock
Aquire a wake lock
show notification
Set alarm for timeout and reenabling keyguard and release wakelock is the user dont touches the screen.
4.1 User touches the screen, and I disable the timer. Do nothing more. Done and done
4.2 User dont touch the screen, so reenable keyguard and release wakelock. Phone sleeps again
Basically I'm wondering about point 4.1 the most. cancel the pendingintent for the alarm, and do nothing more? or should the keyguard and wakelock that are set be dealt with in some way?
The trick to implement your own Keyguard-replacement appears to be the following:-
In the onCreate method, you don't disable the keyguard, but the user can interact with the screen at this point so you need to be careful about accidental touches.
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED |
LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_KEEP_SCREEN_ON);
mKeyguardManager = (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mKeyguardManager.newKeyguardLock(TAG);
If the user performs some action to indicate they want to interact more fully, then we can disable the keyguard and move on.
mKeyguardLock.disableKeyguard();
mKeyguardManager.exitKeyguardSecurely(null);
If they don't, then since the keyguard isn't disabled you shouldn't need to do anything more, just finish your activity
Thats it, but I'm still testing it. So, I'm not 100% sure about it.
I know this question is old but the API clearly state that programatically trying to acquire a keyguard unlock is deprecated.
The correct strategy is, in the oncCreate method of your activity, to have:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Hope this helps others.
Locks are global and when one appplication acquires it it will be off until the lock is released.
You should always reenable locks. Otherwise system won't go sleep or lock from Home or any other application
EDIT: I'm not really shure how it works with keyguard :/
Related
I have following handed-over existing code:
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
Based on here and here, is it to prevent the lock screen to appear? However, I am still able to lock my device. Can anyone advise me what is it for?
From the docs:
public void disableKeyguard()
Disable the keyguard from showing. If the keyguard is currently
showing, hide it. The keyguard will be prevented from showing again
until reenableKeyguard() is called. A good place to call this is from
Activity.onResume() Note: This call has no effect while any
android.app.admin.DevicePolicyManager is enabled that requires a
password.
This method requires the caller to hold the permission
android.Manifest.permission.DISABLE_KEYGUARD.
So it really depends on where exactly you put this snippet exactly, and it definitely not means that you can't see your lock screen anymore. In addition, if you have some security in your lock screen, this snippet can bypass the security measure.
It is used to unlock your screen programmatically. Maybe you also noticed while testing, your lockscreen won't appear until you call reenableKeyguard() again. Although reenableKeyguard() will only work if you also called disableKeyguard() from your application.
is it to prevent the lock screen to appear? However, I am still able to lock my device.
But you do see your lockscreen?
Can anyone advise me what is it for?
Opening a notification from the lockscreen basically does this. It disables your keyguard (lockscreen) and launches the app.
Context: I'm under Android 4.0.4.
The code I'm stating is inside an Activity.
The application is a Cordova application, I'm writing this code inside a plugin.
I think I read all the questions here in StackOverflow and the Android docs.
I want to acquire a wakelock, wake up the device, do some stuff and then, when the wakelock is released, i want the screen to turn off again and let the phone sleep.
I have tried several approaches:
I tried several examples found here to wake up the device.
The only way I managed to turn the device on programatically was aquiring a wakelock with the ACQUIRE_CAUSES_WAKEUP flag. This works fine. The problem comes when I release the wakelock. There's no way I can turn the screen off again. It just stays on forever. I tried with all the WAKELOCKS available and with all the FLAGS available.
Note:I'm not totally happy with this approach because as stated in the docs, the only wakelock that is not deprecated is the partial wakelock, which unfortunately can't be used with this flag.
As I couldn't find any way of letting the screen off using the wakelocks functionality I tried to turn it off programmatically.
I think I tried all the examples found here to turn the screen off, and the only one which worked was:
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
The problem is that using this method I can't turn it on again. When I acquire the wakelock again it just stays off.
Also, when I try to wake up the phone pressing the "home" button it doesnt wake up. It only responds after pressing 3 or 4 times the "power" button. Seems like this approach freezes the phone.
I tried to turn on the screen programmatically before acquiring (and before too) the wakelock, restoring the default brightness value using the same code as above, but It doesn't work.
It's almost a week that I'm struggling with this. Any piece of advice will be appreciated.
I am working on an android project where I am using PowerManager.FULL_WAKE_LOCK to make sure the screen stays on at all times.
I was wondering if the user locks the device while the wake lock has been aquired whether the wakelock will automatically be released or whether I would need to handle this myself.
I did a google search but couldn't come up with anything.
Thanks
You should still have the lock,
please note that once you release the lock the device may instantly go to standby if the timeout time has been reached while you still held the lock. So if some automatic event releases the lock, the screen might instantly turn black.
DUPLICATE WARNING: this post IS NOT about screen on/off, this post IS about keyguard lock (those issues are not the same).
Luckily there is a direct event when the keyguard is unlocked, but what about event when the keyguard is locked? I searched Android API event list back and forth, and I simply don't see it (phrase "lock" -- none, phrase "keyguard" -- one, for off state).
I know the workaround -- wait for screen off event, check in loop the state of keyguard AND wait for screen on event, because the phone can go only in two directions starting from screen off -- either phone will be locked or the screen will be on again. Not huge amount of code, but hack anyway, so I hope I missed something in the manual and there is direct event for keyguard lock.
I am trying to disable the keyguard when SCREEN_ON is trigged. The following code is in a service run by the broadcast receiver of ACTION_SCREEN_ON:
KeyguardManager myKeyGuard = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
myLock = myKeyGuard.newKeyguardLock();
myLock.disableKeyguard();
startActivity("xxx");
This seems to work, but there is a problem. After the screen is on and my activity is displayed bypassing the keyguard, if the user presses the home button, the lockscreen is displayed, and if the user unlocks the screen, the next time the screen switches on, the keyguard is not disabled. I have to manually unlock the phone, and the activity is indeed running behind the lockscreen. So only the disablekeyguard() seems to stop working.
Any help with be appreciated:)
Apparently there is ambiguity in how the Home key events handled by Android. The OP of a post here seems to be facing the same problem. No answers to his query yet.