在 Android 应用程序中实现此行为的最佳策略是什么?

发布于 2024-09-07 20:26:05 字数 366 浏览 4 评论 0原文

在我的 Android 应用程序中,我有一些数据需要每天同步,但也需要在用户使用应用程序时每小时更新一次。

我已经实现了一项服务,该服务从警报中调用以进行每日更新。我在制定每小时同步的策略时遇到问题。我也可以使用每小时警报并触发相同的意图,但是由于您的应用程序可以随时被终止,因此无法取消它(并且由于它们使用相同的意图,因此取消操作将取消所有警报,包括我的警报)每日同步,所以这可能不好)。

另一个选项是使用在应用程序内部设置的计时器,并在应用程序内部触发我的意图。我假设当应用程序被杀死时所有计时器都会被取消,对吧?但我的应用程序由多个活动组成,我希望计时器在所有活动中都起作用,我该怎么做?我不想重复代码 - 我们已经在使用 Activity 和 ListActivity 的子类。

In my Android app, I have some data that needs to be synced daily but also needs to be updated every hour when a user is inside the app.

I have already implemented a service that gets called from an alarm for the daily update. Im having a problem with developing a strategy to do the hourly sync. I could use an hourly alarm too and fire the same intent, but since your app can be killed at any time, there would be no way to cancel it (and since they use the same Intent, doing a cancel would cancel ALL alarms including my daily sync, so that's probably not good).

The other option is to use a Timer that's set when inside the app, and have that fire my Intent when inside the app. Im assuming all Timers get cancelled when an app is killed right? But my app consists of several activities and I want the timer to work across all activities, how do I do that? I dont want to duplicate code - we're already using a subclass for Activity and ListActivity.

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

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

发布评论

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

评论(2

少钕鈤記 2024-09-14 20:26:05

我有一些数据需要
每天同步,但也需要
当用户使用时每小时更新一次
在应用程序内。

解决方案似乎很简单:放弃第二个要求。很少有应用程序会连续使用几个小时,因为人们倾向于将 Android 手机用于其他用途(例如,手机),因此您的每小时更新(如果一直使用)的代码可能永远不会运行。

我也可以使用每小时闹钟
激发同样的意图,但自从你
应用程序可以随时被杀死,那里
没有办法取消它

FWIW,您的应用程序在屏幕上时不会被杀死。而且,虽然它不在屏幕上,但您不希望每小时更新一次。

我假设所有计时器都被取消
当应用程序被杀死时对吗?

应用程序通常不会被“杀死”。我们希望您在使用 onDestroy() 调用您的活动时自行清理。如果您使用守护线程设置Timer,则需要终止该线程。

但是我的应用程序由几个组成
活动,我想要计时器
跨所有活动开展工作,我该如何
这样做吗?

您的每项活动都与您的服务绑定。如果它是由您的警报Intent启动的,请让它执行正常的更新处理。如果它是由于绑定请求而启动的,只需让它确保每小时的Timer 正在运行。当使用 onDestroy() 调用它时(例如,在所有活动解除绑定之后),让它停止 Timer

I have some data that needs to be
synced daily but also needs to be
updated every hour when a user is
inside the app.

The solution seems easy: drop the second requirement. Few apps are used continuously for hours on end, since people tend to use their Android phones for other things (e.g., phones), so your update-hourly-if-used-all-the-time code will probably never run.

I could use an hourly alarm too and
fire the same intent, but since your
app can be killed at any time, there
would be no way to cancel it

FWIW, your app will not be killed while it is on-screen. And, while it is not on-screen, you don't want the updates going hourly.

Im assuming all Timers get cancelled
when an app is killed right?

Apps generally are not "killed". We expect you to clean up after yourself when your activities are called with onDestroy(). If you set up the Timer with a daemon thread, you will need to terminate that thread.

But my app consists of several
activities and I want the timer to
work across all activities, how do I
do that?

Bind to your service from each of your activities. If it is started by your alarm Intent, have it do normal update processing. If it is started due to a binding request, just have it make sure its hourly Timer is running. When it is called with onDestroy() (e.g., after all activities have unbound), have it stop the Timer.

只是一片海 2024-09-14 20:26:05

您也许可以在后台服务中运行计时器(其被杀死的次数少于活动),但仍然不能保证 Android 也不会杀死您的服务。运行这样的后台可能会消耗大量电池。

在 onResume 中创建的后台线程中进行每小时同步怎么样?并且只保存用户上次同步的时间,如果已经> >一个小时就可以完成同步。因为我认为没有任何理由急切地同步用户永远不会看到的数据。

You might be able to have a Timer run in a background service (which get killed less than activities) but there is still no guarantee that Android won't kill your service either. And running something like this is the background might use a lot of battery.

What about doing the hourly sync in a background thread that get's created in onResume? And just save the last time the user did the sync, and if it has been > an hour just do the sync. Because I don't think there is any reason to eagerly sync data that the user is never going to see.

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