Unlock the Android Lockscreen activity - android

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.

Related

Android FLAG_SHOW_WHEN_LOCKED and Keyguard Lock

My application has an activity that is launched by a BroadcastReceiver when android.intent.action.BOOT_COMPLETED broadcast is received.
The activity should be shown full screen over Keyguard (secure). Activity is using Theme.NoTitleBar.Fullscreen theme, set in the app manifest and WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED to put the Keyguard behind.
I am not trying to replace or disable keyguard any way. The activity should just show some diagnostics info about the device and it should remain active until user closes it.
So, the device boots up, keyguard usually appears and the after few seconds activity is also launched. Activity is displayed nicely over the keyguard in full screen mode without title bar. All this worked fine on my Sony and Samsung test devices.
Today I was testing the app on LG G2 device (Android 4.4.2) and noticed a strange issue. The problem is that the fullscreen activity is dismissed (paused) and keyguard is brought up again.
I spent half day going through the code and trying to figure out what is causing this behavior. Then finally I noticed that my activity is dismissed every time when system notification is shown in title bar. In my case the notification is "Battery full" and "Unplug to save battery". I did not had a chance to test, but probably same will happen with other notifications.
Is there a way to suppress system notifications or ignore, so the full screen activity won't be paused?
Try with below code snippets,
Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
this.sendBroadcast(intent);
This is broadcast when a user action should request a temporary system dialog to dismiss. Some examples of temporary system dialogs are the notification window-shade and the recent tasks dialog
Refer ACTION_CLOSE_SYSTEM_DIALOGS

FLAG_DISMISS_KEYGUARD to unlock a screen

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);

Unlock a screen in android using disableKeyguard

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.

disablekeyguard() not working properly when receiver of SCREEN_ON is triggered in Android 2.2?

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.

Android WakeLock and KeyGuard

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 :/

Categories

Resources