2012-02-13 78 views
0

嗨,我正在工作的自定義吐司,我能夠做到這一點,但之後,當我移動到下一個活動線程正在運行或活動的回活動,所以我應該怎麼做做移除該線程或停止此線程。刪除吐司,也摧毀或強制停止線程

我的代碼如下:

public void customToast(int x, int y, String str) { 
    if (Util.tipson == true) { 
     toast = new Toast(getApplicationContext()); 
     toast.setDuration(Toast.LENGTH_SHORT); 
     toast.setGravity(Gravity.TOP, x, y); 
     LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     toastView = li.inflate(R.layout.toastlayout, null); 
     toast.setView(toastView); 
     TextView text = (TextView) toastView.findViewById(R.id.text); 
     text.setText(str); 
     // toast.show(); 

     fireLongToast(); 
    } 

} 

private void fireLongToast() { 

    t = new Thread() { 
     public void run() { 
      int count = 0; 
      try { 
       while (true && count < 40) { 
        try { 
         toast.show(); 
         sleep(100); 
         count++; 
        } catch (Exception e) { 
         // TODO: handle exception 
        } 
        // do some logic that breaks out of the while loop 
       } 

       toast = null; 
       toastView = null; 
      } catch (Exception e) { 
       Log.e("LongToast", "", e); 
      } 
     } 
    }; 
    t.start(); 
} 

回答

0

是否finishing的活動有什麼影響?

+0

thanx for reply,我完成了活動,但它不能阻止thred。 – 2012-02-13 11:29:41

+0

在你創建的線程上調用stop() – Ruuhkis 2012-02-13 11:51:10

0

不知道,但您可以撥打thread的功能join您的活動的onDestroy

+0

連接函數是什麼? – 2012-02-13 11:43:22

+0

告訴你,m不知道,但我從一些教程閱讀...反正檢查這個鏈接... http://developer.android.com/reference/java/lang/Thread.html#join%28%29 – Farhan 2012-02-13 11:46:09

+0

join()與此無關,請使用stop()。 PS。join()讓當前線程等待直到你調用join()的線程完成 – Ruuhkis 2012-02-13 11:51:23

1

你需要自己停止你的線程。由於java不允許你使用stop()函數。

寫類的主題,因爲這

public class YourThread extends Thread { 
     private volatile boolean stopped = false; 

     public void run() { 
      int count = 0; 
      try { 
       while (true && count < 40 && !stopped) { 
        try { 
         toast.show(); 
         sleep(100); 
         count++; 
        } catch (Exception e) { 
         // TODO: handle exception 
        } 
        // do some logic that breaks out of the while loop 
       } 

       toast = null; 
       toastView = null; 
      } catch (Exception e) { 
       Log.e("LongToast", "", e); 
      } 
     } 

     public void stopThread() { 
      stopped = true; 
     } 
    } 

現在,當你的活動它具有線程完成停止你的線程

@Override 
protected void onDestroy() { 
    if(isFinishing()) 
     yourThreadVariable.stopThread(); 
} 
+0

這個時候我們想在一個activity中顯示更多的toast可能會發生問題。你試過這個嗎?請給我溶劑 – 2012-02-13 15:15:02

0

要停止你可以使用interrupt()線程。但爲了更好的解決方案,我會說不要使用Thread。只需使用Runnable創建一個Handler並使用Handler管理您的Runnable,這將是一個不錯的方法,因爲Android已給Handler管理一個或多個Runnables

創建一個Runnable

Runnable runnable = new Runnable() { 

     @Override 
     public void run() { 
      // put your code stuff here 
     } 
    }; 

要啓動的Runnable使用

handler.postDelayed(runnable, your_time_in_millis); 

要停止使用的Runnable

handler.removeCallbacks(runnable); 
0

我想建議拉利特Poptani方法也和實施這樣的:

protected void onStop(){ 
    handler.removeCallbacks(runnable); 
    super.onStop(); 
} 

的方法的文檔:

的onStop,調用活動時沒有對用戶不再可見,因爲另一項活動已經恢復並正在覆蓋這一個。這可能是因爲一項新的活動正在開始,現有的活動正在這個活動之前,或者這個活動正在被摧毀。 http://developer.android.com/reference/android/app/Activity.html