I want to wake up and unlock a device. Then, I'd like show an activity when the user has new message from Firebase.
I wrote this on onResume() method:
window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
This works for devices with API > 19. The problem is that in KitKat, it either does nothing or it wakes up the screen but doesn't unlock the device.
Also I set the right permissions in the AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Also in MyFirebaseMessagingService.class, I'm starting an activity with FLAG_ACTIVITY_NEW_TASK as flag.
Does anyone know what am I missing?
Thanks for the help.
Ok i found it. I just add that piece of code and it worked
KeyguardManager manager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock = manager.newKeyguardLock("abc");
lock.disableKeyguard();
Related
I'm developing an alarm aplication, so when the alarm goes on it start an activity with an off button
It's very simple, but my question, is there a way to start the activity even if the device is locked???
You need to use the WakeLock and also disable your keyboard for the respective Activity. They can be achieved by help of WindowManager.LayoutParams class.
WakeLock allows your app to wake up the screen i.e. turn the display on while your Activity or your complete app is running in the foreground. To learn more about implementing the WakeLock go through the following link:
http://developer.android.com/training/scheduling/wakelock.html
To disable your keyguard you need to use WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED and/or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD flags for the respective Activity.
To know more about them, go to the following links:
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED
You will also need to use the following permissions in the Manifest file of your Android Project:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
You need the following permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Check this link
I am looking for a way to replace the stock lock screen (with an app, not a rom).
What is the best way to do it, for a start to disable the lock screen on as much devices as possible?
Thanks!
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
in androidmanifest:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
You can just use this line in the activity:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Try this, it will keep awake the screen/ display , as long as the activity is on top.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Also this does not require any permission in manifest.
Check out this link http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
listen to the screen on intent and I guess just launch your lock screen.
I have Activity1 which is a list screen of items. Each item can be viewed in a separate Activity2 which is displayed as a popup. Activity1 can be launched from the background and displayed even when the screen is locked. Activity1 may also choose to automatically display the contents of an item in the list screen by starting Activity2. We can bypass the lock screen because both activities have the WindowManagerFlags.DismissKeyguard enabled in the OnCreate method.
Before Android Lollipop everything worked as expected. But now the popup Activity2 is not visible unless the device is manually unlocked. If I change Activity2 to be a full screen Activity then everything seems to work (Except transitioning from one activity to another will briefly display the lock screen). Any ideas on how to fix this cleanly?
Also, I have only tried the Galaxy S6/S6 Edge devices which have this new Knox security feature on them.
Edit I have changed Activity2 to be a DialogFragment instead of an Activity. This worked for me best because the suggested answer used code that is deprecated or obsolete depending on the target sdk. Activity1 is using the following flags to bypass the lock screen when needed.
getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Since Activity2 is now just a DialogFragment, it uses the window flags of the parent Activity1. I also remove those flags on the "android.intent.action.SCREEN_OFF" action so that the activity bypasses the lock screen only when launched as a notification and not every time the activity is at the top of the stack. Permissions mentioned in the answer are required.
For sure this snippet should help You (works with Lollipop):
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
wl.acquire();
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
and when leaving Your activity (i.e. onStop(), onPause() and onDestroy()):
keyguardLock.reenableKeyguard();
Also, don't forget about permissions:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
public void screenTurnALWAYSON() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
I have this code, this keep the screen always on. How can I reset these settings when I don't need it anymore?
With the clearFlags method, f.e.:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Use WakeLocker.acquire(context); and WakeLocker.release();.
you'll need <uses-permission android:name="android.permission.WAKE_LOCK" /> permission.
I'm trying to disable screen lock. I want the screen to go off after timeout, but to go on again with my app when touched.
Since keywardlock is deprecated, I tried the following in onCreate():
final Window win = getWindow();
win.setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Just in case, I added the following in the manifest (is this needed??):
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.FLAG_SHOW_WHEN_LOCKED" />
Still, after my usual timeout, the screen is locked...
What am I doing wrong??
Thanks!
SetFlags takes the flags and a mask you are not using it that way. The easyest thing is to use the helper method addFlags I am using the following code.
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);