对于重复的用户通知,哪个更好:一系列广播、启动的服务还是绑定的服务?

发布于 2024-12-09 13:04:32 字数 1067 浏览 1 评论 0原文

我目前正在开发一个 Android 应用程序,它将以随机确定的时间间隔向用户重复询问问题。这意味着如果需要,它将唤醒设备,弹出一个 Activity,发出声音或振动,并让用户选择其中一个选项。
用户将能够打开或关闭一系列弹出窗口。用户还可以将其暂时关闭一段自行确定的时间。最后,用户将能够指定一天中的指定时间,在这些时间里一系列弹出窗口将自动打开或关闭。

我已经知道如何使用 AlarmManager 安排活动和广播接收器。我还知道如何通过查看 android闹钟源代码

我的问题是如何使用 Android 框架最好地实现它。当弹出系列开始时,我的应用程序需要随机确定下一个弹出窗口将发生的时间间隔。到了显示通知的时间。 Activity 将弹出(已实现),并立即随机计算下一次弹出时间。当用户尚未回答弹出窗口中的问题而已经到了下一个弹出窗口的时间时,应用程序必须知道这一点。应用程序还必须能够确定何时必须按照用户指定启动和停止其弹出窗口。在所有这些点上都必须调用一些东西。
我想知道我是否可以通过广播接收器接收意图的一系列不同操作来最好地完成所有这一切。或者我应该使用服务来完成所有工作。如果我使用服务,最好通过 onStartCommand() 向其发送不同的命令,还是应该绑定到它并通过 Binder 调用方法?

我发现闹钟的来源在这件事上相当令人困惑,我不确定哪种方法是最好的。他们似乎都有自己的优点和缺点。
服务可以有“状态”,但系统可能决定销毁它以释放内存,从而丢失这些数据。预定的广播接收器将被 AlarmManager 记住,但它们是“无状态的”。
通过绑定服务,您可以双向通信,但需要一些时间来获取 Binder。启动的服务可以通过 onStartCommand() 获得快速命令,但不能直接与 Activity 通信。

这个决定依然艰难……

I'm currently developing an android application that will repeatedly ask a question to the user in randomly determined time intervals. This means it will wake up the device if nessecary, pop up an Activity, make a sound or vibration and let the user choose one of the options.
The user will be able to turn the series of pop ups on or off. The user will also be able to temporarily turn it off for a self-determined period of time. Lastly the user will be able to specify designated times of the day in which the series of pop ups will turn on or off automatically.

I already know how to schedule Activities and BroadcastReceivers with AlarmManager. I also know how to wake the device up by looking at the source code for the alarm clock on android.

My question is how to best implement it with the Android framework. When the pop up series start my application needs to randomly determine in which interval the next pop up will occur. When the time has come for the notification to be displayed. The Activity will pop up (already implemented) and immedeately the next pop up time will be randomly calculated. When the user has not answered the question in the pop up when it is already time for the next pop up, the application must know that. The application must also be able to determine when is has to start and stop its pop ups as specified by the user. At all these points something must be called.
I'm wondering if I could best do all this with a BroadcastReceiver receiving a series of different actions by Intent. Or should I use a Service doing all the work. And if I use a service is it best to send it different commands through onStartCommand() or should I rather bind to it and call the methods through the Binder?

I found the source for the alarm clock rather confusing on this matter and I'm unsure which method is the best. They all seem to have their strong and weak points.
Services can have 'states', but the system may decide to destroy it to free memory, thus losing this data. Scheduled BroadcastReceivers will be remembered by the AlarmManager, but they are 'stateless'.
With bound Services you can communicate both ways, but it takes some time to accuire the Binder. Started services can be given quick commands through onStartCommand(), but cannot directly communicate with Activities.

The decision remains hard...

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

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

发布评论

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

评论(1

尽揽少女心 2024-12-16 13:04:32

这个决定仍然很艰难......

只有一个答案:将 AlarmManagerBroadcastReceiver 一起使用。没有其他方法可以可靠地唤醒手机。该 BroadcastReceiver 反过来应该获取 WakeLock 并启动您的 Activity。当 Activity 位于前台时,该 Activity 可以使用 android:keepScreenOn 使设备保持唤醒状态;调用 setContentView() 后,Activity 应在 onCreate() 中释放由 BroadcastReceiver 获取的 WakeLock。该活动可以根据用户输入或缺乏用户输入来安排下一个警报。

The decision remains hard...

There is only one answer: use AlarmManager with a BroadcastReceiver. Nothing else can reliably wake up the phone. That BroadcastReceiver, in turn, should acquire a WakeLock and start up your activity. The activity can use android:keepScreenOn to keep the device awake while the activity is in the foreground; the activity should release the WakeLock acquired by the BroadcastReceiver in onCreate() after the call to setContentView(). The activity can schedule the next alarm based upon user input or lack thereof.

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