how to bring up lock screen in android?

2020-04-21 04:30发布

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?

1条回答
▲ chillily
2楼-- · 2020-04-21 04:49

I also found lots of examples but neither of them brought up the lock screen(with no error)(e.g. this or this)

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 call lockNow().

查看更多
登录 后发表回答