删除线程Toast消息

时间:2012-09-17 16:44:17

标签: android multithreading toast

我正在尝试从标签切换到另一个标签时“删除”Toast,因此假设暂停关注的标签活动。基本上我可以将cancel方法应用于单个Toast对象,或将其指定为null。这很有效。

关键是当我使用TextView并将Toast线程化时,即使我停止了线程,Toast仍然可见。我从this point开始根据this one设置Toast并试图阻止它。

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


致电Toast(这没关系):

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;
    }
}

尝试“删除”Toast(不起作用)

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


}

2 个答案:

答案 0 :(得分:1)

使用handler完成Toast show和dismiss。因此创建toast的线程应该运行直到toast被解除,以便处理程序可以处理dismiss消息。所以在UI线程中创建Toast。

确保在Ui线程中创建mySingleToast

编辑:由于你是在Ui线程中创建toast,看起来,在你的情况下,太多atoast.show()排队了。 LENGTH_SHORT通常显示2秒钟。在这段时间内你增加了近6-7场演出。这就是继续展示的原因。

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

答案 1 :(得分: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 */