countDownタイマーを一時停止する方法と、開始をクリックすると、停止した最後のポイントから開始し、pauseButtonが同じクリックでこのカウントダウンタイマーを一時停止したい場合、カウントアップタイマーも一時停止します。それで、これのコードは何ですか?
public class one extends Activity {
private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
TextView mButtonLabel;
private long mStartTime = 0L;
private TextView mTimeLabel, mTimerLabel;
private Handler mHandler = new Handler();
static final int UPDATE_INTERVAL = 1000;
String timerStop1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
mTimeLabel = (TextView) findViewById(R.id.countDownTv);
mButtonLabel = (TextView) findViewById(R.id.countDown);
// mTimerLabel = (TextView) findViewById(R.id.textTimer);
mButtonLabel = (TextView) findViewById(R.id.countDown);
timerValue = (TextView) findViewById(R.id.timerValue);
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
CountDownTimer timer1 = new CountDownTimer(1800000, 1000) {
@Override
public void onFinish() {
mTimeLabel.setText("Done");
}
@Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
mTimeLabel.setText("" + minutes + ":"
+ String.format("%02d", seconds));
}
}.start();
}
});
pauseButton = (Button) findViewById(R.id.pauseButton);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
// here i want to put the code that will make this click pause the countdown timer also
}
});
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":" + String.format("%02d", secs)
+ ":" + String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
final long start = mStartTime;
long millis = SystemClock.uptimeMillis() - start;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
mTimerLabel.setText("" + minutes + ":"
+ String.format("%02d", seconds));
timerStop1 = minutes + ":" + String.format("%02d", seconds);
mHandler.postDelayed(this, 200);
}
};
}