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: Passing Date in putExra
  • <AudioRecord> “Could not get audio input for record source 1”
  • InApp Billing Security and Remote Method Invocation
  • Return transition not working correctly when using fragment shared transitions
  • Emulator error: This AVD's configuration is missing a kernel file
  • Broadcast Receiver Not Working After Device Reboot in Android
  • DialogFragment with clear background (not dimmed)
  • AlertDialog: How To Remove Black Borders Above and Below View
  • is it possible to create listview inside dialog?
  • Wake Android Device up
  • BroadcastReceiver for ACTION_MEDIA_BUTTON not working
  • When using ActionMode, the status bar turns black on Lollipop
  • 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.