How to lock/unlock the screen with Pattern/Password mode in Android?

I was successful to lock/unlock my screen using DevicePolicyManager and KeyguardManager in Android L. It worked well when I lock/unlock screen using swipe mode (no security). However, I cannot lock/unlock it when I lock/unlock screen by using Pattern and Password mode (higher security). Is it possible to lock/unlock screen with high security using DevicePolicyManager and KeyguardManager. ? This is what I did

protected static final int REQUEST_ENABLE = 0;
DevicePolicyManager devicePolicyManager;
ComponentName adminComponent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.btn);
    button.setOnClickListener(btnListener);

}

//LOCK
Button.OnClickListener btnListener = new Button.OnClickListener() {
    public void onClick(View v) {
        adminComponent = new ComponentName(MainActivity.this, Darclass.class);
        devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

        if (!devicePolicyManager.isAdminActive(adminComponent)) {

            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
            startActivityForResult(intent, REQUEST_ENABLE);
        } else {
            devicePolicyManager.lockNow();
        }

    }
}; 

//UNLOCK
 private KeyguardManager keyguardManager;
 KeyguardManager.KeyguardLock kl;
 keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
 kl = keyguardManager.newKeyguardLock("MyKeyguardLock");
 kl.disableKeyguard();

Note that, I am using it in a service.

  • Android Animation - Flip
  • How to run cordova plugin in Android background service?
  • How to set theme to ProgressDialog?
  • LinearAlloc exceeded capacity (5242880), last=1092 Error while installing on my Samsung Tablet Device
  • Call Firebase database from Google App Engine
  • Android Min SDK Version vs. Target SDK Version
  • Related posts:

    How to set a Button gray and unclickable?
    MultiDex NoClassDefFound error
    Android ACCESS_LOCATION_EXTRA_COMMANDS permission uses
    BroadcastReceiver with multiple filters or multiple BroadcastReceivers?
    What characters allowed in file names on Android?
    How to monitor HTTP (get, post etc) requests that my app is making in android
  • How to remove the title in Dialog?
  • Phone and tablet support for button spacing on the layout view
  • Difference between Kivy and Java for android apps
  • Test if file exists
  • Google reverse image search API
  • Google MapFragment is blank (white) with Could not find class 'maps.j.k'
  • 2 Solutions collect form web for “How to lock/unlock the screen with Pattern/Password mode in Android?”

    The issue is likely to be that you are not calling the window from the context.

    keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    

    Using the following snippet:

    //Get the window from the context
    WindowManager wm = Context.getSystemService(Context.WINDOW_SERVICE);
    
    //Unlock
    //http://developer.android.com/reference/android/app/Activity.html#getWindow()
    Window window = getWindow();  
    window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);  
    
    //Lock device
    DevicePolicyManager mDPM;
    mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    

    With the following caveat:

    Note
    For the following to work you need reference to an activity that can talk to the window object.

    Also related How to Lock/Unlock screen programmatically? and Android screen lock/ unlock programmatically

    You mentioned that you are using the code in a Service, but while constructing your adminComponent ComponentName object you provide MainActivity.this as your context ! MainActivity.this might be NULL if your MainActivity is not currently running.

    I suggest you initiate adminComponent with the Service class as the Context.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.