Sunday, May 20, 2012

Android : CountDownTimer



CountDownTimer is used for Schedule a countdown until a time in the future, with regular notifications on intervals along the way.

CountDownTimer methods:


CountDownTimer Example:

RunOnUiThreadExample.java:


package com.imrankhanandroid.CountDownTimer; import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.widget.TextView; import android.widget.Toast; public class CountDownTimerExample extends Activity { /** Called when the activity is first created. */ private MyCount mc; private TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.show); mc = new MyCount(30000, 1000); mc.start(); } /* Define a countdown internal class */ class MyCount extends CountDownTimer { public MyCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { tv.setText("finish!!!"); } @Override public void onTick(long millisUntilFinished) { tv.setText("Please wait for 30 seconds." + "(" + millisUntilFinished / 1000 + ")..."); //Toast show the time delay Toast.makeText(CountDownTimerExample.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show(); } } }




main.xml:




Download Source Code: CountDownTimerExample








0 comments:

Post a Comment

 
Powered by Blogger