java 在 Android 中定期执行 AsyncTasks 的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3617453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:40:06  来源:igfitidea点击:

Best way to periodically executing AsyncTasks in Android

javaandroid

提问by Kunal P.Bharati

I am getting data from the server using AsyncTask. I need to update the data periodically.

我正在使用AsyncTask. 我需要定期更新数据。

Whats the best way to do it?

最好的方法是什么?

采纳答案by Mathieu

You could use Timer class to schedule periodic task using TimerTask instead of AsyncTask

您可以使用 Timer 类来安排使用 TimerTask 而不是 AsyncTask 的周期性任务

See :

看 :

http://developer.android.com/reference/java/util/Timer.html

http://developer.android.com/reference/java/util/Timer.html

http://developer.android.com/reference/java/util/TimerTask.html

http://developer.android.com/reference/java/util/TimerTask.html

And to update your UI you should follow this good tutorial :

要更新您的用户界面,您应该遵循这个很好的教程:

http://android-developers.blogspot.com/2007/11/stitch-in-time.html

http://android-developers.blogspot.com/2007/11/stitch-in-time.html

回答by Macarse

Set an alarm with AlarmManagerand call your AsyncTaskin your AlarmReceiverclass.

使用AlarmManager设置闹钟并AsyncTask在您的AlarmReceiver班级中呼叫您。

回答by Prizoff

Just to state one of the possibilities. You could also use ScheduledThreadPoolExecutorclass. It has, generally, more possibilities then TimerTask.

只是陈述一种可能性。您还可以使用ScheduledThreadPoolExecutor类。一般来说,它比 TimerTask 有更多的可能性。

回答by user2768

To elaborate upon macarse's response, you can indeed "set an alarm with AlarmManager and call your AsyncTask in your AlarmReceiver class."

要详细说明macarse的响应,您确实可以“使用 AlarmManager 设置警报并在您的 AlarmReceiver 类中调用您的 AsyncTask”。

The concern I raised in the linked post still stands: I don't know if there are any life cycle issues associated with instantiating an AsyncTask from a WakefulBroadcastReceiver, i.e., I don't know if the above solution can lead to MyAsyncTask being killed prematurely.

我在链接帖子中提出的问题仍然存在:我不知道从 WakefulBroadcastReceiver 实例化 AsyncTask 是否存在任何生命周期问题,即,我不知道上述解决方案是否会导致 MyAsyncTask 过早被杀死.

回答by DeRagan

You can do it either on your onProgressUpdate()or on onPostExecute()based on your requirement.

您可以根据您的要求onProgressUpdate()onPostExecute()根据您的要求进行。

onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

onProgressUpdate(Progress...),在调用 publishProgress(Progress...) 后在 UI 线程上调用。执行的时间是不确定的。此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可用于动画进度条或在文本字段中显示日志。

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

onPostExecute(Result), 在后台计算完成后在 UI 线程上调用。后台计算的结果作为参数传递给这一步。

http://developer.android.com/intl/de/reference/android/os/AsyncTask.html

http://developer.android.com/intl/de/reference/android/os/AsyncTask.html