类似问题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+?
发布评论
评论(1)
如果我理解你对,我会有类似的东西。我是我的应用程序,我有一个警报,该警报应在触发警报时打开警报活动。我为解决此问题所做的工作:
1)使用全屏意图:
配置通知时,只需添加此行即可告诉您将使用全屏意图:
2)使用活动上的标志:
告诉系统您的活动将在锁定屏幕上方,
increate()
在调用超级设置此标志之前:在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:
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:Tested on Android 12. Activity is launching