在我的應用程序中,我有幾個Activities
。問題是,當我想關閉一個應用程序時,我的一個活動不會終止,當我重新打開該應用程序時,首先提出該活動。關閉處理程序,刪除回調
就我而言,這可能是使用Handler
的問題。
在這個活動中,我使用Handler
在onTouchListener
旋轉的圖像使用Runnable
,而按下按鈕。釋放按鈕後,我刪除回調並調用另一種方法開始下一個活動。
public void ListenTo(){
MatchTime.setOnTouchListener(new OnTouchListener(){
long time_start=0;
long time_end=0;
@Override
public boolean onTouch(View view, MotionEvent event) {
matchHandler = new Handler();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
time_start=System.currentTimeMillis();
matchHandler.post(matchAction);
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
time_end=System.currentTimeMillis();
long TimeCounted=time_end-time_start;
matchHandler.removeCallbacks(matchAction);
matchHandler = null;
SaveAndSend(TimeCounted);
return true;
}
return false;
}
Runnable matchAction = new Runnable() {
@Override public void run() {
Face = (ImageView) findViewById(R.id.MatchTimeImage);
Face.setRotation(Face.getRotation()+9);
matchHandler.postDelayed(this, 25);
}
};
});
}
我的問題是:
我在做什麼不對的Handler
線程不會停止,直到Android的終止它本身?
你說得對,我的問題不正確。我想知道的是我應該在哪裏/如何創建此Handler?我將matchHandler = new Handler()移到了onCreate上,並得到了和前面相同的結果。 – user3348059
也許你多次設置Listener?每次創建新的偵聽器時,都會創建新的matchAction。我看不到你所有的代碼。但是,如果你使用相同的處理程序和可運行的實例post和removeCallbacks應該可以工作。 – Leonidos
此活動的源代碼:http://pastebin.com/UHUXY3tE – user3348059