Reputation: 608
I've got a night clock app and I want to disable the keyguard for the device while the app is running AND the device is on a charger. The idea is that if you are on a charger and this app is running then it's reasonable to assume that you are in a safe location. When the app is exited (either via back or home) then I do NOT want the unlock screen to appear, just allow normal usage as if the phone was never idle for more than the device's lock time.
Currently my app uses the FLAG_SHOW_WHEN_LOCKED window option and it nicely keeps the screen on and unlocked, but when I hit back or home (after the lock time has expired) I get the unlock keypad screen to unlock the device. I want to prevent this locking of other apps from happening while my app is running and the device is on the charger. I will obviously make this functionality a setting that the user chooses, but how to implement this?
Can anyone help me with this - all my googleing efforts point to keeping only my app from locking, but I've already got that solved.
Thanks.
Upvotes: 4
Views: 611
Reputation: 52956
Non system apps cannot disable the keyguard. Best you can do is make your app a device administrator and increase the time to lock. This, however, might be seen as suspicious for a clock app. Additionally, once the user exits your app, you generally have no control over what is happening with other apps or the system, this is how Android works. I see why it might feel unnatural for the device to suddenly be locked after you press the home key, but I don't think there is anything you can do about it.
Upvotes: 0
Reputation: 4725
First create a intent filter and listen for charger events
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
and in the listener
// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
if(isCharging) {
//disable keyguard
}
and in your onPause()
enable keyguard
Upvotes: 0