androidandroid-activityflagsandroid-windowmanagerunlock

Android activity unlocking screen


I'm trying to add an Activity that makes a locked screen unlock and shows an Activity. Everything works except the unlocking of the screen. The Activity starts but the screen stays off. But I need the screen to go on the same time Activity starts.

I'm using the following code:

public void vibrate() {
    if ((deltaX > vibrateThreshold) || (deltaY > vibrateThreshold) || (deltaZ > vibrateThreshold)) {

            //http://www.androidauthority.com/android-7-0-features-673002/

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("My notification")
                            .setContentText("Hello World!")
                            .setPriority(Notification.PRIORITY_MAX);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


            notificationManager.notify(0, mBuilder.build());

            Intent alarmIntent = new Intent("android.intent.action.MAIN");
            alarmIntent.setClass(this, test.class);
            alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        alarmIntent.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            this.startActivity(alarmIntent);

            new CountDownTimer(25000, 1000) {

                public void onTick(long millisUntilFinished) {
                    //here you can have your logic to set text to edittext
                }
            public void onFinish() {
            }
            }.start();
    }
}

In my AndroidManifest I have the following code:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

Solution

  • Try to use the KeyguardManager

    Example:

    KeyguardManager.KeyguardLock keyguardLock = keyguardLock = ((KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE)).newKeyguardLock("TAG");
    
    keyguardLock.disableKeyguard(); // to unlock the device
    keyguardLock.reenableKeyguard(); // to lock the device, only works if your application called .disableKeyguard before
    

    The permission needed:

    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    

    To wake up the device:

    // Called from onCreate
    protected void createWakeLocks(){
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK");
        partialWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK");
    }
    
    // Called implicitly when device is about to sleep or application is backgrounded
    protected void onPause(){
        super.onPause();
        partialWakeLock.acquire();
    }
    
    // Called implicitly when device is about to wake up or foregrounded
    protected void onResume(){
        super.onResume();
        if(fullWakeLock.isHeld()){
            fullWakeLock.release();
        }
        if(partialWakeLock.isHeld()){
            partialWakeLock.release();
        }
    }
    

    Source: Android - Wake Up and Unlock Device
    Another link: Android: Wake & unlock phone