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
?
-
//Lock device DevicePolicyManager mDPM; mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); – Shakeeb Ayaz Aug 31 '13 at 10:24
-
please add ur code to show what all u tried. Surely u r doing some mistake .. – Shakeeb Ayaz Aug 31 '13 at 10:26
-
@user123 I tried this: http://stackoverflow.com/a/12723269/1986618 – Soheil Aug 31 '13 at 10:42
-
@user123 what is the mistake? – Soheil Aug 31 '13 at 10:43
-
You should add your complete activity code from where the button is being clicked – Shakeeb Ayaz Aug 31 '13 at 10:53
-
@user12 OK, I will do that in hours! thanx for your attention :) – Soheil Aug 31 '13 at 11:03
1 Answers
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()
.

- 986,068
- 189
- 2,389
- 2,491
-
thanks, your code works well. but the only bad thing is this that it turns off the screen, how to prevent this happening? I mean directly bring up the lock screen? – Soheil Aug 31 '13 at 20:49
-