2012-09-17 3 views
1

탭에서 다른 탭으로 전환 할 때 Toast을 "제거"하려고하므로 포커스가있는 탭 작업이 일시 중지되었다고 가정합니다. 기본적으로 취소 메서드를 단일 Toast 개체에 적용하거나 null에 할당 할 수 있습니다. 이 작동합니다.스레드 토스트 메시지 삭제

요점은 TextView를 사용하고 토스트를 스레드 할 때 스레드를 멈추더라도 토스트가 계속 표시된다는 것입니다. 나는 this point에서 시작하여 토스트를 실을 때 this one에 따라 멈추려고합니다.

스레드를 중지하고 시스템 대기열에서 토스트를 제거하는 방법 또는 그 목표를 달성하는 더 좋은 해결책이 있습니까 (예 : 단일 활동의 여러 토스트, 활동에서 다른 토큰으로 교환 할 때 토스트 표시 중지)? 축배를 (이 괜찮습니다)를 호출


:

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 스레드에서 토스트를 만듭니다.

mySingleToast가 UI 스레드에서

편집을 작성해야합니다 : 당신이 UI 스레드에서 토스트를 만들기 때문에, 당신의 경우처럼 (가)를 큐에 너무 많은 atoast.show을 보인다. LENGTH_SHORT는 대개 2 초 동안 표시됩니다. 그리고 그 기간 동안 거의 6-7 편의 프로그램을 추가했습니다. 그것이 계속해서 보여지는 이유입니다.

당신이 할 수있는 일은 aToast.show도 바로 앞에 aToast.cancel()을 추가하는 것입니다.

+0

mySingleToast가 onCreate에서 만들어 졌는지 확인합니다. – hornetbzz