I'm using this code to disable keyboard to disable home button .
I'm using this in activity where I want the keyguard to be disabled ,so is it necessary that keyguard should be called from service ?
If not why keyguard is not getting disabled ?
KeyguardManager km = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
km.inKeyguardRestrictedInputMode();
this.key = km.newKeyguardLock("IN");
key.disableKeyguard();
String s = String.valueOf(km.isKeyguardLocked());
Log.d("keyguardvalue",s);
Please call for activity onResume or onCreate:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
if (keyguardManager != null)
keyguardManager.requestDismissKeyguard(this, null);
}
}
Related
My app never goes to sleep while power is connected and if power is removed then the tablet goes into sleep. After powercable is reinserted the app is getting launched automatically when power is connected (Battery is charging).
To achive this I have implemented a BroadcastReceiver and within this Receiver I (re-) launch my app with:
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
// Start the MainActivity
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity(i);
}
In the MainActivity I use (in onCreate()):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
}
else{
}
if( something...) {
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Object o = this.getSystemService(Context.KEYGUARD_SERVICE);
if (o != null) {
KeyguardManager keyguardManager = (KeyguardManager) o;
keyguardManager.requestDismissKeyguard(this, null);
}
}
else {
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
This works fine as long as the app is not screen-pinned. If the app is screen-pinned then this works only once. I assume because the app gets launched from screen-pinned context ?? Any further plugging of the powercable launches the app but is getting overlayed by a battery charge % window which dissapears after ca. 5 seconds and then the tablet goes into sleep. Is there a difference in this context when the app is screen-pinned ?
I am trying to display my alarm activity over lock screen. I am using a full screen intent from my broadcast receiver. When the screen is locked, the activity is not displayed over lock screen. It is displayed when i unlock the screen. Where am i going wrong?
I have used following permissions:
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
In the activity, i am setting following flags in the activity's oncreate:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
KeyguardManager keyguardManager = (KeyguardManager)
getSystemService(Context.KEYGUARD_SERVICE);
if(keyguardManager!=null)
keyguardManager.requestDismissKeyguard(this, null);
} else {
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
);
}
I've tried opening an Activity on the lock screen in Android Pie but I could not.
I added the showWhenLocked and turnScreenOn on the Android Manifest and on the proper activity and permissions I searched for but could not make the activity open on the default lock screen on the phone. Some help?
MainActivity
if (Build.VERSION.SDK_INT >= 27) {
setShowWhenLocked(true);
setTurnScreenOn(true);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
Android Manifest
android:showWhenLocked="true"
android:turnScreenOn="true"
android:showOnLockScreen="true"
android:excludeFromRecents="true"
Here is my working method call it from onCreate() before setContentView() of activity
Also set below flag for your activity in AndroidManifest file.
android:showOnLockScreen="true"
/**
* Allow calling screen to display over lock screen
*/
private fun allowOnLockScreen() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true)
setTurnScreenOn(true)
val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
keyguardManager.requestDismissKeyguard(this, null)
} else {
this.window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
}
}
Just override the following method in your activity which you want to open on the lock screen.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
And add the following tag to your activity in Manifest.
android:excludeFromRecents="true"
Something like this should work:
PowerManager.WakeLock cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "XXX:CPU_WAKE_LOCK");
cpuWakelock.acquire();
I am trying to disable lock screen while making my screen turn off but as soon as screen turns off lock is held again.
I am using below code to disable the lock screen:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
} else {
KeyguardManager km = (KeyguardManager) activity.getSystemService(KEYGUARD_SERVICE);
kl = km.newKeyguardLock("name");
kl.disableKeyguard();
}
And to enable the lock screen:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
} else {
KeyguardManager km = (KeyguardManager) activity.getSystemService(KEYGUARD_SERVICE);
kl = km.newKeyguardLock("name");
kl.reenableKeyguard();
}
Try this in OnCreate of the Activity.
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);
Please give below permission also in android.manifest.xml
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
You can also refer this link for more info
https://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
I want launch my activity on push notification over lock screen without change in lock.
Any special permission for that activity?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1)
{
setShowWhenLocked(true);
setTurnScreenOn(true);
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if(keyguardManager!=null)
keyguardManager.requestDismissKeyguard(this, null);
}
else
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
After API level 17 this would work
<activity
android:name=".yourActivityName"
android:showOnLockScreen="true"
android:screenOrientation="sensorPortrait" >
or write this in onCreate() before calling setContentView()
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
The other answers include extra functionality that you may or may not want.
The minimum code to allow your activity to display on the lock screen is this:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= 27)
setShowWhenLocked(true);
else
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
// setContentView etc
}
If you also want your activity to turn on the screen (since it might be off) or unlock the keyguard, then see Vladimir Demirev's answer.
In the method onCreate(Bundle savedInstanceState) you should add some window flags:
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);