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

2023-09-05 11:23:37 作者:不再、說永遠

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

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.

在我运行一个的AsyncTask 来运行我的code的服务。据我所知的唯一途径,我可以告诉,如果它失败与否从 onPostExecute()

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?

推荐答案

Falmarri的答案是正确的,我不明白您的问题。

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

onPostExecute(),当你确定事情发生了出错:

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

呼叫 getSystemService(ALARM_SERVICE)获得 AlarmManager 呼叫设置() AlarmManager 来触发你在5分钟内 Call getSystemService(ALARM_SERVICE) to get the AlarmManager Call set() on the AlarmManager to trigger you in 5 minutes

如果需要,请使用额外的意图 PendingIntent 来给你重试什么样的信息,或使用自定义操作字符串从预定的报警,或什么区别重试。需要注意的是,如果你使用意图演员,你需要选择一个合适的标志和 PendingIntent (例如, FLAG_UPDATE_CURRENT )。

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).

该问题的AlarmManager是从一类启动但AsyncTask的是在另一个类,因此   启动该AlarmManager类不知道它是否出现故障或没有。

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.

所以呢?多类可以倾诉的 AlarmManager 。此外,随意通过其构造的数据传递到您的的AsyncTask 的子类。

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

另外,你可能要考虑使用 IntentService ,而不是服务 AsyncTask的 IntentService 自动给你一个后台线程。此外,它关闭时,有没有更多的工作要做,这也是很重要的,这样你就不会得到市场一帮一星评级抱怨你保持运行的所有的时间服务。

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.

我并不想开始我的服务类的AlarmManager从onPostExecute()。

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

为什么不呢?

如果我启动AlarmManager从服务中的排序,然后我创建一个反复出现的函数,其中   我是从自身调用服务。

请问 我用c 写安卓的时候,用AlarmManager写的,但是没反应,代码如下,求大神指点

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.

当然。这就是你想要的。重要的不是无足轻重是否服务的AsyncTask MyOtherReallyCoolClass 是一个真正交谈 AlarmManager - 这是重新编排服务组件服务本身。

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.