Android的计时器任务不会等待预定时间延迟 [英] Android Timer Task doesn't wait for the scheduled delayed time

查看:238
本文介绍了Android的计时器任务不会等待预定时间延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计时器任务不会等待预定延迟时间。
我想通过延迟10秒的网络检查,但没有进行帮助waiting.Any几秒钟内行动将AP preciated。

  INT I = 0;
公共无效的TimerTask()
    {        而(ⅰ小于5){        处理程序处理程序=新的处理程序();
        handler.postDelayed(新的Runnable(){
                公共无效的run(){
                如果(isNetworkConnected())//一些方法来检查网络连接
                 {
                     下载(); //方法下载
                 }
            }
        },10000);        的System.out.println(I =+ I);
        我++;
        }    }


解决方案

试试这个。

 的TimerTask doAsynchronousTask;
    最后的处理程序处理程序=新的处理程序();
    定时器定时器=新的Timer();    doAsynchronousTask =新的TimerTask(){        @覆盖
        公共无效的run(){            handler.post(新的Runnable(){
                公共无效的run(){
                     如果(isOnline){//检查网络连接
                     下载(); //方法下载
                    }                }
            });        }    };    timer.schedule(doAsynchronousTask,0,10000); //在执行每10秒

The timer task doesn't wait for the scheduled delayed time. I want to delay the network check by 10 seconds,but it performs the action within few seconds without waiting.Any help would be appreciated.

int i = 0;
public void timertask()
    {

        while(i < 5){

        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() {
                public void run() {
                if(isNetworkConnected()) // Some method to check net connection
                 {
                     download(); //Method to download
                 }
            }
        }, 10000);

        System.out.println("i  = "+i);
        i++;
        }

    }

解决方案

try this one.

TimerTask doAsynchronousTask;
    final Handler handler = new Handler();
    Timer timer = new Timer();

    doAsynchronousTask = new TimerTask() {

        @Override
        public void run() {

            handler.post(new Runnable() {
                public void run() {
                     if(isOnline){// check net connection
                     download(); //Method to download
                    }

                }
            });

        }

    };

    timer.schedule(doAsynchronousTask, 0, 10000);// execute in every 10 s

这篇关于Android的计时器任务不会等待预定时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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