5分钟的间隔之后开始递归的AsyncTask [英] Starting AsyncTask recursively after a gap of 5 minutes
问题描述
我想创建一个类的实例(它扩展的AsyncTask
),并调用它的的execute()
方法每5分钟后。对于我试着打电话给视频下载(5 * 60 * 1000))
在 onPostExecute()
方法,然后创建类的新实例。在code是如下图所示。
I want to create an instance of a class (which extends Asynctask
) and call its execute()
method after every 5 minutes. For that I tried to call Thread.sleep(5*60*1000))
in onPostExecute()
method and then create a new instance of the class. The code is as below.
public class MyAsyncTask extends AsyncTask<String, Void, String>
{
protected String doInBackground(String... arg0) {
//whatever I want to do
}
protected void onPostExecute(String result) {
Thread.sleep(5*60*1000);
new MyAsyncTask().execute("my String");
}
}
但是使用这个code块的UI 5分钟。我读的地方,在 onPostExecute的code()
在UI线程中执行。这就解释了为什么在UI被阻止。
但后来我怎么创建的AsyncTask
的新实例,而不阻塞UI?
But using this code blocks the UI for 5 minutes. I read somewhere that the code in onPostExecute()
is executed in the UI thread. This explains why the UI is blocked.
But then how do I create a new instance of AsyncTask
without blocking the UI ?
有什么建议?
谢谢你。
Any suggestions ? Thanks.
推荐答案
使用在本onPostExecute code。
Use this code in the onPostExecute.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new MyAsyncTask().execute("my String");
}
}, 5*60*1000);
这篇关于5分钟的间隔之后开始递归的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!