gpt4 book ai didi

android - 向通知添加一个操作以再次显示通知,但稍后,就像通知的贪睡计时器一样?

转载 作者:行者123 更新时间:2023-11-29 15:23:59 25 4
gpt4 key购买 nike

我的应用显示通知。我想通过 addAction 向通知添加一个操作,导致通知消失并在一小时后重新出现,但是我不想为此打开一个 Activity ,最好的情况是:用户单击操作,通知消失并且然后在一小时后重新出现,就像通知的贪睡计时器一样。

有没有办法在不打开 Activity 的情况下做到这一点?

谢谢你的帮助

最佳答案

每个通知操作都来自 PendingIntent。当然,您不想启动 Activity ,也不必这样做。

您的案例的最佳选择是创建您的 PendingIntent:

public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)

这样,每当用户点击通知时,它就会在系统上生成一个广播。然后你必须创建一个 BroadcastReceiver , 以接收此广播并使用 AlarmManager 安排另一个 PendingIntent .第二个未决 Intent 将是一个不同的广播,无论何时触发,您的 BroadcastReceiver 都可以接收并显示新的通知。

有道理吗?

**编辑:

只是为了澄清成一个伪例子:

  • Your app have a BroadcastReceiver registered in the Manifest to receive Broadcasts of 'com.yourapp.mybroadcast'
  • Notification have a com.yourapp.mybroadcast PendingIntent with extras boolean isSchedule = true and int time = 60 (60 minutes)
  • The user taps the notification and this broadcast is fired and your BroadcastReceiver receives onReceive(Context context, Intent intent)
  • Inside your onReceive you read isSchedule=true (meaning it have to create a new schedule and time = 60 minutes. So it uses the AlarmManager to create a new Broadcast PendingIntent with isSchedule = false; to be fired in 60 min
  • The AlarmManager will call your BroadcastReceiver again in 60min but this time with isSchedule = false, so your BroadcastReceiver knows its time to show the notification again.

关于android - 向通知添加一个操作以再次显示通知,但稍后,就像通知的贪睡计时器一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14937225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com