Sanjay Joshi Sanjay Joshi - 8 months ago 46
Android Question

How to lock/unlock phone programmatically : Android

In my application, I need to lock and unlock phone. I searched for it, I found many answers, I tried them but no one is working.

I need to unlock phone on

and lock phone again while I'm finishing my activity.

Do you guys know any method to do the same?

Thanks friends.


Some links, that I have tried are:

How my app can unlock screen programatically?

Android screen lock/ unlock programatically!topic/android-developers/BOLjJTMO4zE

In my application I'm scheduling a task using
and then enabling the phone lock. My activity is getting started on scheduled time but it not unlocking my phone. when I'm unlocking my phone manually running activity is getting appeared

Answer Source
DevicePolicyManager deviceManger;
ActivityManager activityManager;
ComponentName compName;

add code in onCreate()

deviceManger = (DevicePolicyManager)getSystemService(
activityManager = (ActivityManager)getSystemService(
compName = new ComponentName(this, DeviceAdminLock.class);

add code in manifest file

<receiver android:name=".DeviceAdminLock"
        <meta-data android:name=""
            android:resource="@xml/device_admin_sample" />
            <action android:name="" />

Create an xml resource file in res/xml as device_admin_sample.xml and add the following -

<device-admin xmlns:android="">
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
    <expire-password />
    <encrypted-storage />
    <disable-camera />

Add permissions in manifest file -

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Create one receiver class which extends DeviceAdminReceiver class.

public class DeviceAdminLock extends DeviceAdminReceiver {

public void onEnabled(Context context, Intent intent) {

public CharSequence onDisableRequested(Context context, Intent intent) {
    return "This is an optional message to warn the user about disabling.";

public void onDisabled(Context context, Intent intent) {

public void onPasswordChanged(Context context, Intent intent) {

public void onPasswordFailed(Context context, Intent intent) {

public void onPasswordSucceeded(Context context, Intent intent) {


ask activate lock permission in activity class

Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"Additional text explaining why this needs to be added.");
startActivityForResult(intent, 1);

check permission is given or not

boolean active = deviceManger.isAdminActive(compName);
if (active) {
// if available then lock

unlock device


To Unlock

KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock"); 

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                                 | PowerManager.ACQUIRE_CAUSES_WAKEUP
                                 | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");