Search code examples
androidlockinglockscreen

Device Locking in Android


I need to lock my phone without using the device physical button. I created an Activity that contains a Button. Clicking the Button should lock the phone (i.e it should be like this http://www.freeimagehosting.net/2dcaf)

Can any one help me whit this? And the thing is it is not possible using keyguard manager or power manager because it is disabling and enabling the locking system.

Currently the code is done but some bugs are occuring... Here is my code

dpm = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);     
dpm.lockNow();

Solution

  • From 2.2 (I think) you can use DevicePolicyManager:

    DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    

    and then:

    devicePolicyManager.lockNow();
    

    Edit:

    For this solution you have to add a device admin e.g.:

    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);  
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, securemeAdmin);