Friday, September 27, 2013

Count down timer

package com.kundan.timer;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
TextView tv;
int time=10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textView);
 
        final Handler h = new Handler();
        final Runnable r = new Runnable() 
        {
@Override
public void run() 
{


                if(time==0){
                //finish();
    //android.os.Process.killProcess(android.os.Process.myPid());
            
                Toast t=Toast.makeText(getApplicationContext(), "Started", Toast.LENGTH_SHORT);
                t.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
                t.show();
               
                }else{
                time--;
                    tv.setText("Countdown time: "+time+" seconds" );
                }
}
};

Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() 
{
@Override
public void run() 
{
h.post(r);
}
}, 1000, 5000 );




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

No comments:

Post a Comment