实现通知服务的技术
我有一个主要活动,用户可以在其中启用/禁用通知、设置通知间隔以及设置通知间隔将使用的基准时间。通知通常会间隔大约 2 小时触发。经过一定时间后,累加器将达到最大值并且不再需要通知。
实施此类通知计划的标准方法是什么?我尝试使用 postAtTime
在服务内部使用处理程序,但似乎有很多条件可能导致它永远无法运行。我查看了服务内部的计时器,但将手机置于待机状态会停止任何计时器,而且这似乎是一个坏主意。
我遇到的唯一其他选项尚未探索,但它涉及使用 AlarmManager
和 BroadcastReceiver
。我应该放弃该服务并安排重复警报吗?一旦我的累加器达到最大值,我需要能够禁用所有剩余的警报。
感谢您的任何意见。
I have a main activity where the user can enable/disable notifications, set the notification interval, and set the base time the notification interval will use. Notifications will typically trigger about 2 hours from each other. After a certain time, an accumulator will reach a maximum value and notifications will no longer be needed.
What is the standard way of implementing such a notification scheme? I tried using a handler inside of a service using postAtTime
, but it seems that there are a lot of conditions that can cause it to never run. I looked at a timer inside of the service, but putting the phone in standby will stop any timers, plus it just seems like a bad idea.
The only other option I came across I have yet to explore, but it involves using an AlarmManager
and a BroadcastReceiver
. Should I just ditch the service and schedule a repeating alarm instead? I need to be able to disable all remaining alarms once my accumulator has reached max value.
Thanks for any input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您启动一个产生这样的线程的服务会怎么样:
我个人没有做过类似的事情,所以我不确定什么服务可以通知用户或启动活动,但它确实有完整的可用选项参加一项活动,所以是的。
哦,但我突然想到,由于这里的多线程方面,您需要为此使用处理程序。
What if you start a service that spawns a thread like this:
I have not done anything like this personally, so I am not sure what service can do to notify the user or start an activity, but it does have the full panoply of options available to an activity, so yeah.
Oh but it just occured to me you'll need to use a handler for that because of the multithreaded aspect here.
由于我总是有有限数量的通知,并且我可以提前计算经过的时间,因此
AlarmManager
和BroadcastReceiver
的组合似乎工作得很好。以下是我的实现方法:我首先创建了一个
BroadcastReceiver
,然后创建了一个类,该类使用
AlarmManager
来创建/取消向BroadcastReceiver 发送消息的警报
重要的部分是使用累加器作为
requestCode
,以便我们稍后可以取消所有警报。最后,我通过在
onCreate()
中调用refreshAlarms()
以及每当用户修改与计划相关的首选项时在活动中使用NotificationSender
类通知。重新启动手机将清除所有警报,因此必须重新启动应用程序才能开始通知。如果系统碰巧杀死了该进程,警报仍然会在适当的时间触发。Since I will always have a finite number of notifications and I can calculate the elapsed time in advance, it seems the combination of
AlarmManager
and aBroadcastReceiver
work pretty well. Here is how I implemented this:I first created a
BroadcastReceiver
I then created a class that used a
AlarmManager
to create/cancel alarms that send a message to theBroadcastReceiver
The important part is to use the accumulator as the
requestCode
so we can cancel all of our alarms later.Finally I used the
NotificationSender
class in my activity by callingrefreshAlarms()
inonCreate()
and whenever the user modifies preferences that are relevant to scheduling notifications. Rebooting the phone will clear all alarms so the app must be restarted before notifications will begin. If the system happens to kills the process, the alarms will still trigger at the appropriate time.