Ho do i lock my android phone programmatically ? I tried following this example. But when i click on the enable button the Activity pops up for few milliseconds and then closes automatically
The log shows no error just this log
Log.i("DeviceAdminSample", "Admin enable FAILED!");
Can any one tell me how to lock the android screen (Like the lock when make to many attempts in pattern lock and the phone locks down)
Any help is appreciated
You have to make your app as admin, Read something over here
Create a new empty project and create a class called
MyAdminReceiver
that extendsDeviceAdminReceiver
like thisCreate a new folder called xml and create an .xml file for your admin rights called
admin.xml
and add the policies, in you case its locking the screenIn your manifest add the receiver under Application tag
And in your
MainActivity.java
add code like thisNote: If you try to call the Intent for Admin Device other that from an Activity subclass there are chances you might get an error to use
Intent.FLAG_ACTIVITY_NEW_TASK
but when you use that your window might not pop like in your case so Try opening it from a subclass of an activity onlyAlso you cannot un-install your app unless it has not be unregistered as an admin
The code shown in that sample will bring up the Settings application when clicked.
You use the code that you linked to. Here is my sample app showing the same basic thing.
Specifically:
You need to have a
BroadcastReceiver
in your manifest that is set up to be a device admin componentThe user has to activate your app as a device admin
You then call
lockNow()
onDevicePolicyManager
Here is the developer documentation on the device admin APIs.