在打ze模式下启动时,活动保持黑色

时间:2018-10-30 12:33:26

标签: android android-activity alarmmanager android-notifications android-doze

因此我有一个警报活动,该警报活动是从服务启动的,并且在打do模式下活动保持黑色,直到我将其关闭并再次打开。< / p>

现在,尽管屏幕为黑色,但视图是有响应的(如果我触摸它们,它们会起作用)

如果我尝试拍摄屏幕截图,我会突然看到活动,并且如果我关闭屏幕并再次打开,一切都按预期进行。

奇怪的视频

Responsive black screen

Turn off, and on again

Trying to take a screenshot

这基本上就是我启动活动的方式:

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(...)

这是我通过Activity的onCreate方法解锁屏幕的方式

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;
    }


}

我设法重现了问题

adb shell dumpsys deviceidle force-idle

我自然无法发布所有代码,但请告诉我您是否需要任何内容​​。

谢谢!

1 个答案:

答案 0 :(得分:1)

从您的if语句中删除return

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