Unlock the Screen Programmatically
Asked Answered
I

1

12

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

Inductive answered 26/6, 2015 at 6:56 Comment(0)
S
8

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.

Schaaf answered 26/4, 2017 at 5:44 Comment(2)
I actually tried this and it works. I used it for a push notification to open a screen automatically, even if the screen is turned off and locked. Thanks.Derwin
Works well, thanks!!, just to complement, some of this code is now deprecated, you can handle this here #48277802Dispensatory

© 2022 - 2024 — McMap. All rights reserved.