Prevent device lock in android during my activity AND the device is connected to the charger

I have an application for night hours and I want to disable keyguard for the device while the application is running. And the device is on the charger. The idea is that if you are on a charger and this application works, then it is reasonable to assume that you are in a safe place. When the application exits (either through the back or home), I DO NOT want the unlock screen to appear, just allow normal use, as if the phone had never worked in standby mode longer than the deviceโ€™s lock time.

Currently my application uses the FLAG_SHOW_WHEN_LOCKED window option and it keeps the screen on and unlocked perfectly, but when I hit back or home (after the lock time has expired), I get a keyboard unlock screen to unlock the device. I want to prevent this blocking of other applications while my application and device are running on the charger. I will obviously make this functionality the setting that the user selects, but how to implement it?

Can someone help me with this - all my googleing attempts indicate that I will only block my application, but I have already decided that this is allowed.

Thanks.

+4
source share
2 answers

First create an intent filter and listen to the events of the charger.

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

0
source

Non-system applications cannot disable key lock. The best you can do is make your application a device administrator and increase the lock time. This, however, can be considered suspicious for the application of the watch. In addition, as soon as the user leaves your application, you usually do not have control over what happens with other applications or the system, this is how Android works. I understand why this might seem unnatural if the device suddenly locks up after clicking on the master key, but I donโ€™t think you can do something about it.

0
source

Source: https://habr.com/ru/post/1439039/


All Articles