TimerHandlerActivity.java
Dosyayı İndir
package com.godoro.androidclients;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class TimerHandlerActivity extends Activity {
private Button startStopButton;
private Button pauseResumeButton;
private TextView elapsedTimeText;
private boolean started=false;
private boolean paused=false;
private int elapsedSeconds=0;
private int durationSeconds=10;
private Handler handler = new Handler();
private boolean durationFixed=true;
private Runnable currentRunnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer_handler);
elapsedTimeText = (TextView) findViewById(R.id.elapsedTimeText);
startStopButton = (Button) findViewById(R.id.startStopButton);
pauseResumeButton = (Button) findViewById(R.id.pauseResumeButton);
}
public void onClickStartStop(View view) {
elapsedTimeText.setText(Long.toString(elapsedSeconds));
if(!started){
startTimer();
}else{
stopTimer();
}
}
public void onClickPauseResume(View view) {
if(!paused){
paused=true;
pauseResumeButton.setText(R.string.resume);
}else{
paused=false;
pauseResumeButton.setText(R.string.pause);
}
}
private void startTimer(){
elapsedSeconds=0;
elapsedTimeText.setText(Integer.toString(elapsedSeconds));
started=true;
startStopButton.setText(R.string.stop);
currentRunnable = new Runnable() {
@Override
public void run() {
if(paused){
handler.postDelayed(this, 1000);
return;
}
elapsedSeconds += 1;
showTime();
if (elapsedSeconds >= durationSeconds && durationFixed) {
stopTimer();
} else {
handler.postDelayed(this, 1000);
}
}
};
handler.removeCallbacks(currentRunnable);
handler.postDelayed(currentRunnable, 1000);
}
private void showTime(){
elapsedTimeText.setText(Integer.toString(elapsedSeconds));
}
private void stopTimer(){
handler.removeCallbacks(currentRunnable);
started=false;
startStopButton.setText(R.string.start);
String message="Oturum bitti : "+elapsedSeconds+"/"+durationSeconds;
Toast.makeText(this,message,Toast.LENGTH_LONG).show();
}
}
Dosyayı İndir