2012-09-17 98 views
1

我试图在从一个标签切换到另一个标签时“删除”Toast,因此假设暂停焦点选项卡活动。基本上我可以将取消方法应用于单个Toast对象,或将其分配给null。这工作。删除线程Toast消息

问题是,当我使用TextView和线程Toast时,即使停止线程,Toast仍然可见。我从this point开始线程吐司,并试图根据this one停止它。

如何停止线程并从系统队列中删除吐司,还是有更好的解决方案来实现该目标(例如,在单个活动中多次吐司,当从活动交换到另一个时停止显示吐司)?


调用吐司(这是OK):

private void showSingleToast(String toastText, int color, long duration_ms) { 


    if (mySingleToast instanceof Toast) { 
     mySingleToast.setText(toastText); 

     if (null != toastView) 
      if (toastView instanceof TextView) 
       toastView.setTextColor(color); 

     ToastExpander.showFor(mySingleToast, duration_ms); 
    } 
} 

穿线Toast.show()

public class ToastExpander extends Thread { 

    public static final String TAG = "ToastExpander"; 

    static Thread t;// = null; 

    // Must be volatile: 
    static volatile boolean stopFlag = false; 
    static long scan_freq = 300; 

    public static void showFor(final Toast aToast, final long durationInMilliseconds) { 

     aToast.setDuration(Toast.LENGTH_SHORT); 

     t = new Thread() { 
      long timeElapsed = 0l; 

      public void run() { 
       try { 
        while (timeElapsed <= durationInMilliseconds || !stopFlag) { 
         long start = System.currentTimeMillis(); 
         aToast.show(); 
         sleep(scan_freq); 
         timeElapsed += System.currentTimeMillis() - start; 
        } 

        // doesn't work: aToast.cancel(); 

       } catch (InterruptedException e) { 
        Log.e(TAG, e.toString()); 
       } 
      } /* end of "run" */ 

     }; /* end of Thread */ 

     t.start(); 



    } /* end showFor */ 

    public static void cancel() { 
    stopFlag = true; 
    } 
} 

试图 “去除” 的吐司(不起作用)

private void hideSingleToast() { 
    //hideTextView(toastView); 
    toastView = null; 
    ToastExpander.cancel(); 
    mySingleToast.cancel(); 
    //mySingleToast= null; 


} 

回答

0

这是我的工作解决方案。

public static void show(final Toast aToast, final String usr_msg, final long durationInMilliseconds, 
      final TextView myView, final int myColor) { 

    // Instanciate Toast 
    aToast.setText(usr_msg); 
    aToast.setDuration(Toast.LENGTH_SHORT); 

    // Instanciate TV 
    myView.setTextColor(myColor); 

    t = new Thread() { 
     long timeElapsed = 0l; 

     public void run() { 

      try { 

       // Make sure view exists 

       // Show Toast in the view 
       while (!stopFlag) { 
        long start = System.currentTimeMillis(); 
        aToast.show(); 
        sleep(scan_freq); 
        timeElapsed += System.currentTimeMillis() - start; 

        // DEBUG 
        if (debug) 
         Log.e(TAG, "|__ Thread running since : " + timeElapsed + " ms"); 

        if (timeElapsed >= durationInMilliseconds) { 
         stopFlag = true; 
         if (debug) 
          Log.e(TAG, "|__ Time elapsed - Process stopped"); 
        } 

       } 




      } catch (InterruptedException e) { 
       Log.e(TAG, "Catch : " + e.toString()); 
      } 
     } /* end of "run" */ 

    }; /* end of Thread */ 

    t.start(); 

    // reset stopFlag for next Toast 
    stopFlag = false; 

} /* end showFor */ 
1

吐司显示和解雇使用handler完成。因此,创建敬酒的线程应该运行,直到敬酒被解散,以便处理程序可以处理解雇消息。所以在UI线程中创建Toast。

确保mySingleToast在UI线程创建的

编辑:既然你正在创建UI线程的面包,看起来像,你的情况太多atoast.show()排队等候。 LENGTH_SHORT通常显示2秒钟。在此期间,您已添加了近6-7场演出。这就是它继续被展示的原因。

您可以做的是在aToast.show之前加上aToast.cancel()

+0

我确认mySingleToast是在onCreate中创建的。 – hornetbzz