1

タイマー タスクは、スケジュールされた遅延時間を待ちません。ネットワークチェックを10秒遅らせたいのですが、待たずに数秒でアクションを実行してくれます。

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++;
        }

    }
4

1 に答える 1

1

これを試してください。

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
于 2012-03-15T11:02:57.517 に答える