CountDownTimer

The android.os.CountDownTimer class schedule a countdown until a time in the future, with regular notifications on intervals along the way.

CountDownTimer

The Constructor CountDownTimer(long millisInFuture, long countDownInterval) call with the number of millis in the future from the call to start() until the countdown is done and onFinish() is called(millisInFuture) and The interval along the way to receive onTick(long) callbacks(countDownInterval).

package com.CountDownTimer;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.widget.TextView;

public class AnCountDownTimerActivity extends Activity {
public class MyCounter extends CountDownTimer {

public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}

@Override
public void onFinish() {
// TODO Auto-generated method stub
myCounterPanel.setText("DONE");
}

@Override
public void onTick(long millisUntilFinished) {

long minutes = millisUntilFinished/60000;
long rSeconds = millisUntilFinished%60000;
long seconds = rSeconds/1000;
long millisonds = rSeconds%1000;

myCounterPanel.setText(minutes + ":" + seconds + ":" + millisonds);

}

}

MyCounter myCounter;
TextView myCounterPanel;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

myCounterPanel = new TextView(this);
myCounterPanel.setTextSize(40);
myCounterPanel.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
myCounterPanel.setGravity(Gravity.CENTER);
setContentView(myCounterPanel);

myCounter = new MyCounter(3000000, 500);
myCounter.start();
}

}

0 Response to "CountDownTimer"

Posting Komentar