public class MainActivity extends ActionBarActivity { Button panic; String TimeHand="s"; Timer timer; final Handler handler = new Handler(); TimerTask timerTask; int k=5,l=2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); panic=(Button)findViewById(R.id.button); panic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (panic.getText().equals("Panic")) { panic.setText("Disable Panic"); Toast.makeText(getApplicationContext(), "2 Seconds", Toast.LENGTH_LONG).show(); timer.cancel(); startTimer(); initializeTimerTask(); timer.schedule(timerTask,0,l*1000); } else if (panic.getText()=="Disable Panic") { panic.setText("Panic"); Toast.makeText(getApplicationContext(), "60 Seconds", Toast.LENGTH_LONG).show(); timer.schedule(timerTask,0, k*1000); } } }); } @Override protected void onResume() { super.onResume(); //onResume we start our timer so it can start when the app comes from the background startTimer(); } public void startTimer() { //set a new Timer timer = new Timer(); //initialize the TimerTask's job initializeTimerTask(); timer.schedule(timerTask, 0, k * 1000); // } public void stoptimertask(View v) { //stop the timer, if it's not already null if (timer != null) { timer.cancel(); timer = null; } } public void reScheduleTimer(int t) { timer = new Timer("alertTimer",true); timerTask = new MyTimerTask(); timer.schedule(timerTask, 0, t * 1000); } private class MyTimerTask extends TimerTask { @Override public void run() { } } public void initializeTimerTask() { timerTask = new TimerTask() { public void run() { //use a handler to run a toast that shows the current timestamp handler.post(new Runnable() { public void run() { //get the current timeStamp Calendar calendar = Calendar.getInstance(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a"); final String strDate = simpleDateFormat.format(calendar.getTime()); //show the toast int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(getApplicationContext(), strDate, duration); toast.show(); } }); } }; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)