【问题标题】:Android Asynk Task run in threadAndroid异步任务在线程中运行
【发布时间】:2016-10-05 23:10:18
【问题描述】:

我想每 5 秒运行一次异步任务,直到 5 分钟,我该怎么做? 我能够每 5 秒运行一次异步任务,但不能限制 5 分钟

public void callAsynchronousTask() {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        new CheckTxnStatusSTResult().execute(final_verification_card);
                        Log.d("dinesh","execute in every five seconds"+ final_verification_card);
                    } catch (Exception e) {

                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 15000);

}

【问题讨论】:

  • 该任务将每 15 秒运行一次,顺便说一句
  • 哦,对不起!但我需要运行到 5 分钟
  • 如果我可以问,你为什么使用 handler.post?

标签: android multithreading android-asynctask


【解决方案1】:

你可以这样做

public void FunctionName() {
    new CountDownTimer(Total_time, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            MyUtils.Log("execute in every one seconds");
            // Display Data by Every Ten Second
        }

        @Override
        public void onFinish() {
           // Call Other function on Function
        }

    }.start();
}

【讨论】:

  • 无法得到想要的结果
  • 对不起各位!此代码运行 asynktask 直到 infinte 。永不停止
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-06
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 2011-12-10
  • 1970-01-01
相关资源
最近更新 更多