0

I want to make an Android app that starts counting down from 10 mins and when it reaches 0, it should execute a command to send data via internet. After that it should start all over again from 10 mins. It should keep sending data every 10 mins until I manually stop it.

(I have already created command to send data via internet and it works perfectly, but I don't know how to make the count down timer.)

Thanks in advance!

4

2 回答 2

2
public class MyCounter extends CountDownTimer
{

    public MyCounter(long millisInFuture, long countDownInterval) 
    {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() 
    {
        System.out.println("Timer Completed.");  
    }

    @Override
    public void onTick(long millisUntilFinished) 
        {
          //Your function here..
        }
}

之前oncreate

final MyCounter timer = new MyCounter(Long.MAX_VALUE,600000);

oncreate,

timer.start();
于 2012-12-28T12:11:37.357 回答
0

您可以在java中使用多线程的概念,创建一个线程并使其休眠几分钟,因为它唤醒调用代码/方法,您可以启动线程

class SampleThread extends Thread {
    @Override
    public void run() {
        try {
            Thread.sleep(600000);
            callYourmethodHere();
        } 
                     catch (InterruptedException e) {
                        } 

    }

SampleThread t = new SampleThread();
    t.start();

在需要时启动计时器

于 2012-12-28T13:07:17.020 回答