ストップウォッチを持っていますが、正常に動作しますが、ホームボタンまたはボタンクロージングベルを押してしばらくしてから再度開くと、ストップウォッチの時刻が間違っています。この問題を解決するために私にできることを教えてください。
public class Stopwatch extends Activity {
Timer mTimer;
int second_f_p = 0, second_s_p = 0, millisecond = 0;
int minute_f_p = 0, minute_s_p;
int hour_f_p = 0, hour_s_p = 0;
int numberOfLap = 0;
TextView tv;
TextView TVlap;
Button btn_start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
btn_start = (Button) this.findViewById(R.id.button1);
tv = (TextView) this.findViewById(R.id.tvTime);
btn_start.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View arg0) {
btn_start.setClickable(false);
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
timerMethod();
}
}, 0, 100);
}
});
Button btn_stop = (Button) this.findViewById(R.id.button2);
btn_stop.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
btn_start.setClickable(true);
mTimer.cancel();
}
});
Button btn_reset = (Button) this.findViewById(R.id.button4);
btn_reset.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
second_f_p = 0;
second_s_p = 0;
minute_f_p = 0;
minute_s_p = 0;
millisecond = 0;
hour_f_p = 0;
hour_s_p = 0;
numberOfLap = 0;
TVlap.setText("");
tv.setText(String.valueOf(hour_f_p) + String.valueOf(hour_s_p) + ":" + String.valueOf(minute_f_p) + String.valueOf(minute_s_p) + ":" + String.valueOf(second_f_p) + String.valueOf(second_s_p) + "." + String.valueOf(millisecond));
}
});
TVlap = (TextView) this.findViewById(R.id.tvLap);
Button btn_lap = (Button) this.findViewById(R.id.button3);
btn_lap.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
numberOfLap++;
TVlap.setText(TVlap.getText() + String.valueOf(numberOfLap) + ". " + String.valueOf(hour_f_p) + String.valueOf(hour_s_p) + ":" + String.valueOf(minute_f_p) + String.valueOf(minute_s_p) + ":" + String.valueOf(second_f_p) + String.valueOf(second_s_p) + "." + String.valueOf(millisecond) + "\n");
}
});
}
private void timerMethod() {
this.runOnUiThread(doSomething);
}
private final Runnable doSomething = new Runnable() {@Override
public void run() {
millisecond++;
if (millisecond == 10) {
second_s_p++;
millisecond = 0;
}
if (second_s_p == 10) {
second_f_p++;
second_s_p = 0;
}
if (second_f_p == 6) {
minute_s_p++;
second_f_p = 0;
}
if (minute_s_p == 10) {
minute_f_p++;
minute_s_p = 0;
}
if (minute_f_p == 6) {
hour_s_p++;
minute_f_p = 0;
}
if (hour_s_p == 10) {
hour_f_p++;
hour_s_p = 0;
}
if (hour_f_p == 9 && hour_s_p == 9) {
second_f_p = 0;
second_s_p = 0;
minute_f_p = 0;
minute_s_p = 0;
hour_f_p = 0;
hour_s_p = 0;
}
tv.setText(String.valueOf(hour_f_p) + String.valueOf(hour_s_p) + ":" + String.valueOf(minute_f_p) + String.valueOf(minute_s_p) + ":" + String.valueOf(second_f_p) + String.valueOf(second_s_p) + "." + String.valueOf(millisecond));
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_stopwatch, menu);
return true;
}
}