androidandroid-5.0-lollipopandroid-wake-lockkeyguard

Lollipop keyguard not dismissed?


I'm developing a app that in certain times it should wake up the device and dismiss the keyguard to display some activity until the user dismisses it. So, the

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

does not work on Android Lollipop, meanwhile it works perfectly on the pre Android versions !! Any suggestions?


Solution

  • This is the working solution:

    BroadcastReceiver Class

    @Override
    public void onReceive(Context context, Intent intent) {
        mWakeLock = ((PowerManager) context.getApplicationContext().getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
        if (!mWakeLock.isHeld()) {
            mWakeLock.acquire();
        }
        Intent intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        mWakeLock.release();
    }
    

    MainActivity Class

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_FULLSCREEN |
                        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.activity);
    ....
    }
    

    And now it is working even on Android Lollipop 5.0.1