我想要一个计时器来计算用户完成任务所需的时间。从关于 stackoverflow 的其他问题中,我设计了以下方法(参见下面的代码)。我的问题-> 有没有更有效的方法来做到这一点?好像有点麻烦。
private Timer myTimer;
private SharedPreferences prefs;
private String prefName = "MyPref";
private static final String TIMER_KEY = "timer";
private static final String FINAL_TIMER_KEY = "final timer";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crossword1);
//Start the timer ticking
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//here i plan on using a shared preference to keep track of the time
//so each "Timer_Tick" would get the latest "TIMER_KEY", add a second to it
//and then re-store it in the shared preferences
}
};