I am using FLAG_DISMISS_KEYGUARD to disable a keygaurd. This is working only when my app is on foreground. But android documents syas that
Use FLAG_DISMISS_KEYGUARD and/or FLAG_SHOW_WHEN_LOCKED instead; this
allows you to seamlessly hide the keyguard as your application moves
in and out of the foreground and does not require that any special
permissions be requested
this is not happening in my application. Then how to use the above flag so that my application should lock the screen when my app moves in and out of the foreground?
Any one have idea?
From the Javadoc for WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD:
Window flag: when set the window will cause the keyguard to be dismissed, only if it is not a secure lock keyguard. Because such a keyguard is not needed for security, it will never re-appear if the user navigates to another window (in contrast to FLAG_SHOW_WHEN_LOCKED, which will only temporarily hide both secure and non-secure keyguards but ensure they reappear when the user moves to another UI that doesn't hide them). If the keyguard is currently active and is secure (requires an unlock pattern) than the user will still need to confirm it before seeing this window, unless FLAG_SHOW_WHEN_LOCKED has also been set.
It sounds like in your case you'd only want to use FLAG_SHOW_WHEN_LOCKED and not FLAG_DISMISS_KEYGUARD.
final Window win= getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Related
I want to create an alert application. The alert is received using GCM (GoogleCloudMessaging). Now I want to show the alert message on the screen without any actions by the user. Therefore I need to wake up the device and unlock the screen.
In the forum I found this answer (and some similar) describing how I can achieve this behaviour:
android-wake-up-and-unlock-device
Reading the documentation, using WakeLocks seems to be deprecated: i.e.
full_wake_lock
FULL_WAKE_LOCK Added in API level 1
int FULL_WAKE_LOCK
This constant was deprecated in API level 17. Most applications should
use FLAG_KEEP_SCREEN_ON instead of this type of wake lock, as it will
be correctly managed by the platform as the user moves between
applications and doesn't require a special permission.
In the documentation an alternative is shown using the WindowManager.
With help of the flags
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
I should be able to bring my alert message to the front without asking for the keyguard secure pattern.
My question now is:
When I want to use these flags, do I also need a wakeLock to wake up my device or is this done implicit by the window manager?
I want to ask about the disableKeyguard() method in KeyguardManager.KeyguardLock class
The documentation says :
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. Note: This call has no effect
while any DevicePolicyManager is enabled that requires a password.
However this class is deprecated since API level 13
Alternate method is to use flags from Window manager
FLAG_DISMISS_KEYGUARD and/or FLAG_SHOW_WHEN_LOCKED
Now these flags work only if lock mode is Swipe
In case of secure lock like PIN or password it can mostly bring one window above
lock screen, but as soon as user moves to another window, the lock reappears.
I want to ask that is the same true for disableKeyGuard() too.
Is it only effective in case of Swipe but not for secure locks like PIN / Password / Face detection etc.
In my application I am using disableKeyguard and reenableKeyguard to Unlock and lock a screen. my application is working fine. But problem is when user unexpectedly unlocked a screen by dragging keygaurd (without using my application), the screen does't lock again with my application.
How to solve this problem?
Thanks in advace
Use FLAG_DISMISS_KEYGUARD and/or FLAG_SHOW_WHEN_LOCKED instead; this allows you to seamlessly hide the keyguard as your application moves in and out of the foreground and does not require that any special permissions be requested. Handle returned by newKeyguardLock(String) that allows you to disable / reenable the keyguard.
I have a running android Service. It runs even on the lockscreen,
and i want it to somehow Unlock the lockscreen.
How can i 'interfere' with the lockscreen activity or whatever the lockscreen is?
Update:
I want my service to Unlock the lockscreen, just like if the user slided the unlock thing.
Got it?
You can. Use this code:
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
This will unlock the Keyguard automatically.
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 :/