Android 中 AlarmManager 行为的澄清

发布于 2024-10-18 09:18:01 字数 449 浏览 6 评论 0原文

我看到了由活动设置 AlarmManager 的所有示例。

我的问题是这样的: 如果我的应用程序设置了一个重复的 AlarmManager,即使在启动的应用程序关闭并从内存中删除后,该情况是否仍然存在?

如果没有,我如何在 Android 启动时启动的较低级别启动 AlarmManager,如果它失败或死机或抛出异常,则重新启动,而无需用户执行任何操作?

最后,如果我希望 BroadcastReceiver 执行的操作没有可视组件,我是否还需要为其创建一个单独的 Activity?就我而言,我希望有一个后台上传器唤醒并查看文件夹,如果它看到该文件夹​​中的文件,则将它们发送到服务器。我不需要给用户任何反馈。

因此,我的理想是拥有一个神奇的、基于操作系统的 AlarmManager,它调用仅处理上传的 IntentService,但我不清楚如何首先运行这样的 AlarmManager。

TIA

I see all the examples of AlarmManager being set by an Activity.

My question is this:
If my application sets a recurring AlarmManager, does that persist even after the application that started is is closed and removed from memory?

If not, how do I start an AlarmManager at a lower level that is started by Android at boot up and if it ever fails or dies or throws an exception is restarted without the user having to do anything?

Lastly, if the action I wish for the BroadcastReceiver to undertake has no visual components, do I still have to create a separate Activity for it? In my case I want there to be a background uploader that wakes up and looks into a folder and if it sees files in that folder, sends them off to the server. I don't need any feedback to the user.

So, my ideal would be to have a magical, OS based AlarmManager that calls an IntentService which just handles the uploading, but I'm unclear on how to get such an AlarmManager running in the first place.

TIA

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

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

发布评论

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

评论(2

赴月观长安 2024-10-25 09:18:01

是的,据我所知,即使在注册警报的活动结束后,警报也会“生存”并不断被触发。但它们无法在手机重启后幸存。

如果我正确理解你的问题,我认为你可以通过创建一个带有广播接收器的项目来实现你想要的目标,该接收器侦听 android.intent.action.BOOT_COMPLETED 意图,然后(重新)注册一个重复警报,该警报依次启动进行上传的(意图)服务。

您不需要活动,但无论如何您可能都需要一个活动,让用户通过选中复选框或其他东西来暂时禁用上传机制。让用户选择警报的频率也可能会很好,即应该启动服务并查找要上传的新文件的频率。这也是第一次注册闹钟的好地方。

Yes, AFAIK the alarms "survive" and keeps getting triggered, even after the activity that registered them ends. But they don't survive a phone reboot.

If I understands your problem correctly, I think you can achieve what your looking for by creating a project with a broadcast receiver that listens for android.intent.action.BOOT_COMPLETED intents and then (re-)register a repeating alarm, which in turns starts a (Intent)Service to do the uploading.

You don't need an activity, but you probably would want one anyway, to let the user temporarily disable the upload mechanism by checking off a checkbox, or something. It would probably also be nice to let the user choose the frequency of your alarm, i.e. how often the service should be started and look for new files to upload. This would also be a good place to register your alarm for the first time.

暗地喜欢 2024-10-25 09:18:01

我同意 Nicolai 的观点,你的应用程序中有 2 个广播接收器:

  • 一个在启动时重新注册警报,
  • 一个在警报触发时启动你的服务

你仍然可以有一个活动,但它不应该由警报接收器(因此是服务):相反,可以在启动服务时启动通知,用户可以从通知的扩展消息启动活动。

也许还可以考虑为您的警报设置 setInexactRepeating (而不是 setRepeating),以及使用工作线程来处理长上传(以防用户想同时在主线程中使用您的活动)。

I agree with Nicolai that you'd have 2 broadcast receivers in your application :

  • one that re-register the alarm on boot
  • one that starts your service when triggered by the alarm

You could still have an activity but it shouldn't be started by the alarm receiver (hence the service) : instead, maybe launch a notification as you start your service, with the user having the possibility to launch the activity from the expanded message of the notification.

maybe also think about setInexactRepeating (instead of setRepeating) for your alarm, as well as using a worker thread to handle the long uploads (in case the user wants to use your activity in the main thread at the same time).

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