java - 当编辑文本焦点发生变化时,如何在特定时间间隔(约 120 秒)后自动保存

标签 java android

我正在开发一个应用程序,类似于记事本应用程序,专注于音乐家。当他们将编辑文本置于焦点(触摸文本框)时,我想每 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/

相关文章:

javascript - 使用 phonegap 和 Sencha Touch 访问 Android 手机联系人

java - Android 无法访问更改系统设置权限

Android NDK 在 JNI 中使用来自 c 代码的 .so 库

android - 有哪些解决方案来保证服务器端和移动客户端通信的安全?

android - 将蓝牙设备与React-Native配对

java - 无法使用 Eclipse 和 Selenium webdriver Java 定位并切换到 iframe

java - Informix java.sql.SQLException : Column (. ..) 在查询中的任何表中都找不到(或 SLV 未定义)

java - SmartGWT 模态窗口

java - 将 String 转换为 charArray 需要什么?

java - 如何在 Apache Commons Mail Api 上设置 Internet 代理?