如何使用 AlarmManager 在 AsyncTask 中下载失败时运行重试

发布于 2024-09-18 23:08:19 字数 200 浏览 4 评论 0原文

我使用 AlarmManager 来运行尝试从网络下载文件的服务。如果失败,我想在 5 分钟内重试该服务。

在服务内部,我运行一个 AsyncTask 来运行我的代码。据我所知,判断它是否失败的唯一方法是通过 onPostExecute()

再次重试该服务的最佳方法是什么?

I use an AlarmManager to run a Service which tries to download a file from the web. If it fails I'd like to retry that Service in 5 minutes.

Inside the Service I run an AsyncTask to run my code. As far as I know the only way I can tell if it failed or not is from the onPostExecute().

What is the best way of implementing a retry of that Service again?

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

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

发布评论

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

评论(1

甜妞爱困 2024-09-25 23:08:20

法尔马里的答案是正确的,我不明白你的担忧。

onPostExecute() 中,当您确定出现问题时:

  1. 调用 getSystemService(ALARM_SERVICE) 获取 AlarmManager
  2. 调用 set() 上的 AlarmManager 会在 5 分钟内触发您

如果需要,请在 PendingIntent 中的 Intent 上使用额外内容,为您提供以下信息:要重试什么,或者使用自定义操作字符串来区分重试和计划的警报等。请注意,如果您使用Intent额外功能,则需要使用PendingIntent选择适当的标志(例如FLAG_UPDATE_CURRENT)。

问题是 AlarmManager 是从一个类启动的,但 AsyncTask 是在另一个类中,因此
启动 AlarmManager 的类不知道它是否失败。

所以?多个类可以与 AlarmManager 通信。另外,请随意通过其构造函数将数据传递给您的 AsyncTask 子类。

此外,您可能需要考虑使用 IntentService 而不是 ServiceAsyncTaskIntentService 自动为您提供后台线程。另外,当没有更多的工作要做时,它就会关闭,这也很重要,这样你就不会在市场上得到一堆一星评级来抱怨你一直运行的服务。

我不想从 Service 类的 onPostExecute() 启动 AlarmManager。

为什么不呢?

如果我从服务中启动 AlarmManager,那么我就创建了一个重复函数,其中
我正在从自身调用服务。

当然。这就是你想要的。实际与 AlarmManager 对话的是 ServiceAsyncTask 还是 MyOtherReallyCoolClass 并不重要 - - 重新调度Service 的组件是Service 本身。

Falmarri's answer is the correct one, and I do not understand your concerns.

In onPostExecute(), when you determine that things went awry:

  1. Call getSystemService(ALARM_SERVICE) to get the AlarmManager
  2. Call set() on the AlarmManager to trigger you in 5 minutes

If needed, use extras on the Intent in the PendingIntent to give you information about what to retry, or use a custom action string to distinguish retries from the scheduled alarm, or something. Note that if you use Intent extras, you will need to choose an appropriate flag with PendingIntent (e.g., FLAG_UPDATE_CURRENT).

The problem is the AlarmManager is started from one class but the AsyncTask is in another class, therefore
the class that starts the AlarmManager doesn't know whether it has failed or not.

So? Multiple classes can talk to the AlarmManager. Also, feel free to pass data to your AsyncTask subclass via its constructor.

Also, you might want to consider using an IntentService rather than a Service and AsyncTask. IntentService gives you a background thread automatically. Plus, it shuts down when there is no more work to be done, which is also important, so you don't get a bunch of one-star ratings on the Market complaining about the service you keep running all of the time.

I don't want to start an AlarmManager from the onPostExecute() of my Service class.

Why not?

If I start an AlarmManager from within the Service then I'm sort of creating a recurring function where
I'm calling the Service from itself.

Of course. That's what you want. It matters not a whit whether the Service or the AsyncTask or the MyOtherReallyCoolClass is the one actually talking to AlarmManager -- the component that is rescheduling the Service is the Service itself.

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