I want to bring up the lock screen of the device if a button is clicked. I searched a lot and know that could be done by DevicePolicyManager
but don't know how to do that. I also found lots of examples but neither of them brought up the lock screen(with no error)(e.g. this or this)
how to lock device using DevicePolicyManager
?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How can I create this custom Bottom Navigation on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
That second link should work. It looks similar to my LockMeNow sample app, which I know works, as I just demonstrated it in a training class two days ago.
You need to implement a
DeviceAdminReceiver
whose manifest entry contains a<meta-data>
link to a policy document that says that you want the ability to<force-lock />
the device.Then, the user needs to agree to this, by going into Settings > Security > Device Administrators and enable your app as a device administrator. There is code in both my sample and the one that you linked to that contains code that checks to see if the app is a device administrator, and if not leads the user into Settings to go enable it.
Once that is done, you can obtain a
DevicePolicyManager
and calllockNow()
.