5分钟的间隔之后开始递归的AsyncTask [英] Starting AsyncTask recursively after a gap of 5 minutes

查看:194
本文介绍了5分钟的间隔之后开始递归的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个类的实例(它扩展的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆