0

一开始,我想为我糟糕的英语道歉。

我的问题是:

下面的计时器每 3 秒显示一次消息,但是当我从程序中退出时(使用后退按钮),消息仍然会弹出。

 public Runnable mUpdateTimeTask = new Runnable(){        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(3000);}
                 catch (Exception e) {
                    // TODO: handle exception
                }
            mHandler.post(new Runnable(){
        @Override
         public void run() {
                            Toast.makeText(getApplicationContext(), "Testing 3 Seconds", Toast.LENGTH_LONG).show();
                  }
              });
            }
        }

};

我已经使用了mHandler.removeCallbacks(mUpdateTimeTask);仍然无法阻止它。

提前致谢。

4

2 回答 2

0

我最近遇到了同样的问题,经过大量搜索,我在这里找到了解决方案。

handler.removeCallbacksAndMessages(null);

只会停止挂起的消息(Runnables)。它不适用于当前运行的可运行文件。您必须编写自己的类来停止当前正在运行的可运行文件。

于 2013-07-31T17:44:11.077 回答
0

The call has been made from a runnable thread class, which is still active when Back button is pressed. It will pop messages until the application destroys. Instead, use this:

    handler.removeCallbacksAndMessages(null);

In the docs for removeCallbacksAndMessages it says. here is the link: developer website documentation link

"Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed."

于 2013-07-31T17:35:49.743 回答