通知蹦床限制 - 在锁定屏幕中开放活动而无需解锁

发布于 2025-01-22 09:34:48 字数 1608 浏览 3 评论 0 原文

类似问题1

https://stackoverflow.com/questions/69238026/android-12-notification-trampoline-restrictions"> similar问题2

tl; dr; dr: 如何执行通知-Action(单击)lock-screen?现在工作了,因为现在您无法从服务或广播接收器中 startActivity()。关于如何实现这一目标有什么建议吗?

详细信息

我有一个应用程序,必须在锁定屏幕顶部打开活动。 在针对Android 12之前,我可以在通知中添加一个操作按钮:

val intent = Intent(this, NotificationReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, flags)
val builder = NotificationCompat.Builder(context, channelId)
builder.addAction(drawable, buttonText, pendingIntent)

pendingIntent.getBroadcast()在“通知操作”按钮上效果很好,因为即使设备已解锁,也会触发广播。

然后, NotificationReceiver 类将在不解锁手机的情况下获取广播,并且它将从那里打开锁定屏幕顶部的活动。

Android 12介绍 startActivity()。我可以使用 PENGENDINTENT.getActivity 而不是 pendingIntent.getBroadcast 并直接启动活动,但是问题是 GetActivity 才直到之后才触发手机已解锁(您单击“通知操作”按钮>它请求解锁设备一旦解锁,它就开始了活动)

任何想法)关于如何在不解锁手机定位Android 12+的情况下从通知中打开活动的任何想法?

Similar questions 1

Similar question 2

TL;DR: Answers to How to perform notification-action (click) on lock-screen? don't work anymore cuz now you can't startActivity() from services or broadcast receivers. Any suggestions on how to achieve this?

DETAILS

I have an app that must open an activity on top of the lock screen.
Before targeting Android 12, I could just add an action button to the notification:

val intent = Intent(this, NotificationReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, flags)
val builder = NotificationCompat.Builder(context, channelId)
builder.addAction(drawable, buttonText, pendingIntent)

pendingIntent.getBroadcast() worked great on the notification action button, because the broadcast will be fired even if the device is unlocked.

The NotificationReceiver class would then get the broadcast WITHOUT unlocking the phone, and it would open the activity on top of the lock screen from there.

Android 12 introduced Notification Trampoline restrictions, now we cannot call startActivity() from broadcast receivers or services. I could use pendingIntent.getActivity instead of pendingIntent.getBroadcast and launch the Activity directly, but the problem is that getActivity isn't triggered until AFTER the phone is unlocked (you click on the notification action button > it requests to unlock device > once unlocked, it starts the activity)

Any idea on how to open an activity from a notification without unlocking your phone targeting Android 12+?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫林﹌晚霞¤ 2025-01-29 09:34:48

如果我理解你对,我会有类似的东西。我是我的应用程序,我有一个警报,该警报应在触发警报时打开警报活动。我为解决此问题所做的工作:

1)使用全屏意图:

配置通知时,只需添加此行即可告诉您将使用全屏意图:

val pendingIntent = //Pending intent of the activity to launch
.setFullScreenIntent(pendingIntent)

2)使用活动上的标志:

告诉系统您的活动将在锁定屏幕上方, increate()在调用超级设置此标志之前:

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

在Android 12上进行测试。活动正在启动

If I understood you right, I have something similar. Im my app I have an alarm which should open the alarm activity when alarm triggered. What I did to solve this problem:

1) Use full screen intents:

When you configure your notification, just add this line to tell that you are going to use full screen intent:

val pendingIntent = //Pending intent of the activity to launch
.setFullScreenIntent(pendingIntent)

2) Use flag on your activity:

To tell the system that your activity is going to appear above the lock screen, in your onCreate() before you call super set this flags:

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

Tested on Android 12. Activity is launching

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文