androidandroid-activityalarmmanagerandroid-notificationsandroid-doze

Activity Remains BLACK when launching under Doze Mode


So I have an alarm activity, that launches from a service, and while in doze mode, the activity remains black until I turn it off and on again.

Now, although the screen is black, the views are responsive (If I touch them, they work)

If I try to take a screenshot, I suddenly see the activity, and if I turn the screen off and on again, everything is working as expected.

Videos of the weirdness:

Responsive black screen

Turn off, and on again

Trying to take a screenshot

This is basically how I launch the activity:

val builder = NotificationCompat.Builder(context, NotificationChannels.CHANNEL_ALARM_SERVICE)
...
// set intent
val alarmIntent = Intent(context, AlarmActivity::class.java)
alarmIntent.putExtra(IntentExtras.KEY_ID, alarm.id)
builder.setFullScreenIntent(PendingIntent.getActivity(context, RequestCodes.REQUEST_CODE_ALARM_ACTIVITY, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT), true)
...
startForeground(...)

This is how I unlock the screen in the Activity's onCreate method

public class ScreenUnlockerUtil {

public static void unlockScreen(BaseActivity activity) {

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        // in addition to flags
        activity.setShowWhenLocked(true);
        activity.setTurnScreenOn(true);
        return;
    }


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        KeyguardManager keyguardManager = (KeyguardManager) activity.getSystemService(KEYGUARD_SERVICE);
        if (keyguardManager != null) {
            keyguardManager.requestDismissKeyguard(activity, new KeyguardManager.KeyguardDismissCallback() {
                @Override
                public void onDismissError() {
                    super.onDismissError();
                    Timber.e("Keyguard Dismiss Error");
                }

                @Override
                public void onDismissSucceeded() {
                    super.onDismissSucceeded();
                    Timber.d("Keyguard Dismiss Success");
                }

                @Override
                public void onDismissCancelled() {
                    super.onDismissCancelled();
                    Timber.w("Keyguard Dismiss Cancelled");
                }
            });
        }
        return;
    }


}

I managed to reproduce the issue with

adb shell dumpsys deviceidle force-idle

I naturally can't post ALL the code, but let me know if there's anything you might need.

Thanks!


Solution

  • Remove the return from your if statement:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        // in addition to flags
        activity.setShowWhenLocked(true);
        activity.setTurnScreenOn(true);
        //return;
    }