简体   繁体   English

Android,如何在锁定屏幕上启动 Activity

[英]Android, How to launch Activity over Lock screen

I want launch my activity on push notification over lock screen without change in lock.我想在锁定屏幕上通过推送通知启动我的活动而不更改锁定。

Any special permission for that activity?该活动有什么特别许可吗?

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1)
    {
        setShowWhenLocked(true);
        setTurnScreenOn(true);
        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        if(keyguardManager!=null)
            keyguardManager.requestDismissKeyguard(this, null);
    }
    else 
    {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    }

After API level 17 this would work在 API 级别 17 之后,这将起作用

<activity
    android:name=".yourActivityName"
    android:showOnLockScreen="true"
    android:screenOrientation="sensorPortrait" >

or write this in onCreate() before calling setContentView()或在调用setContentView()之前将其写入 onCreate setContentView()

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

The other answers include extra functionality that you may or may not want.其他答案包括您可能需要也可能不需要的额外功能。

The minimum code to allow your activity to display on the lock screen is this:允许您的活动在锁定屏幕上显示的最低代码是:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= 27)
        setShowWhenLocked(true);
    else
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

    // setContentView etc
}

If you also want your activity to turn on the screen (since it might be off) or unlock the keyguard, then see Vladimir Demirev's answer .如果您还希望您的活动打开屏幕(因为它可能已关闭)或解锁键盘锁,请参阅Vladimir Demirev 的回答

In the method onCreate(Bundle savedInstanceState) you should add some window flags:在 onCreate(Bundle savedInstanceState) 方法中,您应该添加一些窗口标志:

Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM