1

我正在尝试在锁定屏幕上显示 DialogFragment。当用户通过新的 keyguard 小部件功能触发操作时,这必须发生。

特别是它显示了一个时间选择器对话框,但用户只有在解锁屏幕时才能看到它。即使屏幕被锁定,我如何设法显示它?

我已经尝试了这些标志的所有组合,但没有成功:

AlertDialog dialog = dBuilder.create();
    Window window = dialog.getWindow();

    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            + WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

这在 onCreateView 中调用。

谢谢你的支持!

4

3 回答 3

1

Dialog windows do not show on the lock screen because they are floating windows, I can't give you an actual reason as to why Android doesn't show them, but it might have to do with drawing the underlying lock screen components, since those are, for all intents and purposes "secure components".

Having dealt with your problem before, what I ended up doing was making a separate activity, setting your above window flags to the Activity's window usingActivity.getWindow().setFlags() then showing my DialogFragment. It isn't pretty, but it seems like the closest thing that will reliably work when set up properly. Luckily UI-wise you can set the Activity's theme to be Theme.Wallpaper.NoTitleBar which looks pretty good.

于 2012-12-07T03:49:17.047 回答
1

如果您使用对话框片段,则此代码有效。

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.requestWindowFeature(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    Window window = dialog.getWindow();

    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            + WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    return dialog;
}
于 2017-09-13T13:20:39.770 回答
0
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        PowerManager powerMgr = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerMgr.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,
                "noti");
        KeyguardManager keyMgr = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        keyLock = keyMgr.newKeyguardLock("noti");
keyLock.disableKeyguard();

wakeLock.acquire();

抱歉,代码格式错误。

于 2013-10-09T02:56:41.817 回答