Android:倒数计时器

Android: CountDownTimer

我正在尝试在经过一定时间后发送消息(如代码中所示的 SMS)。但我不认为 CountDownTimer 工作正常,因为它会在显示通知后立即发送第一条消息。

下面是代码

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),MainActivity2Activity.class);

    Notification notification = new Notification(R.mipmap.ic_launcher,"This is a test message!", System.currentTimeMillis());
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

    mManager.notify(0, notification);

    CountDownTimer waitTimer;
    waitTimer = new CountDownTimer(180000, 60000) {

        public void onTick(long millisUntilFinished) {
            sendmessage();
        }

        public void onFinish() {
            //After 60000 milliseconds (60 sec) finish current
            //if you would like to execute something when time finishes
        }
    }.start();


}

那么我怎样才能让计时器正常等待呢?因为正如我所说,它会在调用通知后立即发送第一条消息。

注意:如果用户点击通知,它会打开一个 activity,这应该会终止 CountDownTimer,但我无法实现。怎么做到的?我找不到任何相关信息。

观察 CountDownTimer 表明第一次 onTick() 在调用 onStart() 之后立即被调用,然后在指定的指定时间间隔后调用。

其次,传递的第一个参数 180000 告诉计时器在 180000 毫秒后调用 onFinish() 并在 60000 毫秒后调用 onTick() 立即开始。如果你想限制它不在第一次发送消息,你可以像这样做这个简单的检查:

CountDownTimer waitTimer;
waitTimer = new CountDownTimer(180000, 60000) {
        boolean firstTime = true;
        public void onTick(long millisUntilFinished) {
            if (firstTime) {
                firstTime = false;
                return;
            }

            sendmessage();
        }
    }

    public void onFinish() {
        //After 180000 milliseconds finish current
        //if you would like to execute something when time finishes
    }
}.start();

您也可以使用 TimerTask 执行相同的操作:

Timer waitingTimer = new Timer();
waitingTimer.schedule(new TimerTask() {
    @Override
    public void run() {
        sendMessage();
        // Add a base condition here to cancel the task if needed..
    }
}, 60000, 180000);

你可以用下面的代码来实现,把intervalTime作为你等待的时间,以毫秒为单位。

Timer waitingTimer = new Timer();
waitingTimer.schedule(new TimerTask() {
    @Override
    public void run() {
        sendMessage();
        waitingTimer.cancel();
    }
}, intervalTime, 5000);

您可以为此使用 Timer 和 TimerTask。您可以将延迟设置为 运行 并且对于重复,您可以设置一个时间,之后它应该再次执行。

示例:

Timer timer = null;

public void scheduleTimer (){
    cancelTimer(); // Cancel scheduled timer if any.
    timer = new Timer("message_sender");
    timer.schedule(new TimerTask() {

      @Override
      public void run() {
        //This timer will execute timertask with first delay of 2 seconds and repeat it every 3 seconds.
      }
    }, 2000, 3000);
}

//Method for cancelling any scheduled timer
private void cancelTimer(){
    if (timer != null){
        timer.cancel();
        timer.purge();
        timer = null;
    }
}

如果您想执行任何 UI 级别的任务,那么在 运行() 中您需要使用 runOnUiThread

来执行代码