我正在开发一个应用程序,类似于记事本应用程序,专注于音乐家。当他们将编辑文本置于焦点(触摸文本框)时,我想每 2 分钟自动保存一次编辑文本框中的文本内容。我尝试使用 CountDownTimer,但我需要帮助每 2 分钟循环一次编辑文本,并在调用应用的 onPause() 时终止它。
new CountDownTimer(120000, 1000) {
public void onTick(long millisUntilFinished) {
//every 1000 millis
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
//when 2 minutes is reached
mTextField.setText("done!");
}
}.start();
我可以通过再次调用 start() 在 onFinish() 中重新启动它,但随后它就会陷入后台的无限循环中。谢谢!
最佳答案
I have an app I am working on that focuses, on similar to a notepad app, for musicians. I would like to auto-save their text content every 2 minutes from an edit text box when they put the edit text in focus (touch the text box).
为此,您可以使用计时器 -
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@SuppressWarnings("unchecked")
public void run() {
try {
"Your function call "
}
catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, "Timer value");
当你的工作完成后,你可以通过以下方式停止计时器 -
if(timer != null) {
timer.cancel();
timer = null;
}
希望这有帮助!
关于java - 当编辑文本焦点发生变化时,如何在特定时间间隔(约 120 秒)后自动保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26857445/