【问题标题】:Start Activity screen even if screen is locked in Android即使屏幕在 Android 中被锁定,也启动 Activity 屏幕
【发布时间】:2013-11-21 05:26:03
【问题描述】:

即使屏幕被锁定,如何在设备上启动 Activity。我尝试如下,但它不起作用。

广播接收器:

Intent alarmIntent = new Intent("android.intent.action.MAIN");
        alarmIntent.setClass(context, Alarm.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);
        context.startActivity(alarmIntent);

【问题讨论】:

    标签: android android-activity broadcastreceiver screen-lock


    【解决方案1】:

    您可以通过两种方式实现这一目标:

    1. 按照@Yup 在这篇文章中的说明使用唤醒锁。

    2. 使用窗口标志。

    使用窗口标志:

    onReceive(...) 中打开要启动的Activity A。将此粘贴到该活动 A 的 onCreate()

    final Window win= getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    

    确保您没有在 setContentView(...) 之前粘贴它:-)

    【讨论】:

    • 如果我使用第二种方式(使用窗口标志),我不需要在 androidmanifest.xml 中请求权限?
    • 是的,你没有@Dika的权限
    • 当屏幕被锁定时它也对我有用,它会启动我的活动,但没有调用 onCreate 方法中的其他工作。例如。我正在播放的声音在屏幕锁定时不会播放。
    • 当我评论 mpPlay.stop();从 onPause
    • @Junaid 感谢您的回复,实际上我通过在 onResume 中添加 mp.start 方法代替 onCreate 来管理它,因为最后一次调用 onResume if (mpPlay.isPlaying()) { mpPlay.停止(); mpPlay.reset(); } 在 onDestroy 方法中。使用此屏幕锁定场景活动时打开。
    【解决方案2】:

    您需要AndroidManifest.xml 文件中的以下权限:

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

    检查清单详细信息here。您可以在查询时查看此link

    【讨论】:

      【解决方案3】:

      将此粘贴​​到您要在屏幕锁定时打开的活动的 onCreate 方法中,在 setContentView() 之后

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

      【讨论】:

      • 对我来说,它永远不会到达 onCreate 方法,在此之前有些东西会阻止它
      【解决方案4】:

      您可以查看here屏幕是否锁定或解锁。

      然后您可以使用唤醒锁定和电源管理选项来保持屏幕不被锁定。你可以找到帮助here

      【讨论】:

        【解决方案5】:

        从 Android 版本 10(SDK 版本 29)开始,如果应用在后台运行,其他答案将不再有效,例如在 BroadcastReceiver 中。

        为了使其在 Android 10 及更高版本上运行,如果您确实需要从后台启动 Activity,则应使用全屏 Intent [source]:

        Android 10(API 级别 29)及更高版本对应用在后台运行时何时可以启动 Activity 施加了限制。这些限制有助于最大限度地减少对用户的干扰,并让用户更好地控制屏幕上显示的内容。

        几乎在所有情况下,后台应用都应显示时间敏感的通知,以向用户提供紧急信息,而不是直接启动活动。何时使用此类通知的示例包括处理来电或活动闹钟。

        这可以实现如下[source]:

        val fullScreenIntent = Intent(this, CallActivity::class.java)
        val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
            fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
        
        val notificationBuilder =
                NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("Incoming call")
            .setContentText("(919) 555-1234")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_CALL)
        
            // Use a full-screen intent only for the highest-priority alerts where you
            // have an associated activity that you would like to launch after the user
            // interacts with the notification. Also, if your app targets Android 10
            // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
            // order for the platform to invoke this notification.
            .setFullScreenIntent(fullScreenPendingIntent, true)
        
        val incomingCallNotification = notificationBuilder.build()
        

        此答案的部分内容是从创建的作品和shared by the Android Open Source Project 复制的,并根据Creative Commons 2.5 Attribution License 中描述的术语使用。

        【讨论】:

          【解决方案6】:
          1. 清单文件授予权限 使用权限 android:name="android.permission.WAKE_LOCK" 然后在您的需求活动 onCreate() 中编写代码
          2. 最终窗口赢= getWindow(); win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

          【讨论】:

            【解决方案7】:

            对于某些 Android 8,9,

            您需要在 Manifest 中使用SYSTME_ALERT_WINDOW 权限和 添加标志以启动活动意图.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

            喜欢这个

             val i = Intent(context, AlarmActivity::class.java)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
             it.startActivity(i)
            

            这是在 Kotlin 中

            适用于 Android 10+ 设备

            您需要使用带有待处理意图的全屏意图通知来启动您想要的活动。

            详细了解全屏意图

            Fullscreen Intent Example

            【讨论】:

              猜你喜欢
              • 2013-01-11
              • 1970-01-01
              • 2021-04-01
              • 1970-01-01
              • 2012-05-30
              • 2020-06-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多