Unlock the Screen Programmatically

19k views Asked by At

I have a share button in the GCM notification. On click of the share button, I need to launch share intent. Everything works perfectly. Only problem that I'm facing is Lollipop lock screen feature. When I click share button from lock screen, my intent dialog appears below the lock screen and user has to unlock the screen to see the dialog. I want to unlock the screen programatically, when share button is clicked.

I tried with Power Manager, But all it's wakeClock flags are deprecated and WindowManager.LayoutParams.Flag_KEEP_SCREEN_ONis recommened to use. But I'm not using activity here. I'm using broadcastReciever context. and hence I cannot use getWindow()method.

I also tried with KeyguardManager. But even disableKeyguard() is deprectated.

I cannot use the Intent.ACTION_SCREEN_ON, as this should be used, if we want to perform any action after screen is unlocked.

i had used below intent to programmatically close the notification tray:

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        mContext.sendBroadcast(it);

Is there a similar intent, that can be broadcasted to unlock the screen

Updated Code using DevicePolicyManager:

public static void handleShareBtnClick(Context context, String message) {
    GcmHelper helper = new GcmHelper();
    helper.shareMessage(context, message);
    if(Utility.isLollypopAndAbove()){
          helper.unlockLockScreen();
    }
    helper.launchShareforForAlert();

}



   public void unlockLockScreen(){
        DevicePolicyManager devicePolicyMngr= (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName compName=new ComponentName(mContext, DeviceAdminReceiver.class);
        if(!devicePolicyMngr.isAdminActive(compName))
            devicePolicyMngr.removeActiveAdmin(compName);
    }

Even after using DevicePolicyManager, It's not unlocking my screen

1

There are 1 answers

2
shailesh Rohit On

Step 1: Add below code in your activity before setContentView(R.layout.example);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

Step 2: Lock your mobile, then you will see activity in which you have added this code. This will work even though your mobile is locked with pattern lock. This will work like a charm.