Android Toast封装工具类(可在异线程中弹出)

sancaiodm Android源码摘录 2023-04-16 505 0

记录Android源码中一个封装的Toast工具类,可以非UI线程中的弹出提示,可直接复现使用,


import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.yourapp.ApplicationUtils;

public final class MtkToast {
    private static Toast sToast = null;
    private static final String DEFAULT_TOAST_STRING = "Androidos.net";
    private static final String TAG = "MTKToast";

    public static final int LENGTH_LONG = Toast.LENGTH_LONG;
    public static final int LENGTH_SHORT = Toast.LENGTH_SHORT;

    private MtkToast() {
        // do nothing
    }

    /**
     * Toast with default duration,LENGTH_SHORT
     *
     * @param context
     * @param msg
     *            The message to toast
     */
    public static void toast(Context context, String msg) {
        toast(context, msg, LENGTH_SHORT);
    }

    /**
     * Toast with default duration,LENGTH_SHORT
     *
     * @param context
     * @param resId
     *            The string id.
     */
    public static void toast(Context context, int resId) {
        toast(context, resId, LENGTH_SHORT);
    }

    /**
     *
     * @param context
     * @param resId
     *            The string id
     * @param duration
     *            The toast duration
     */
    public static void toast(Context context, int resId, int duration) {
        toast(context, context.getResources().getString(resId), LENGTH_SHORT);
    }

    /**
     *
     * @param context
     * @param msg
     *            The message to toast
     * @param duration
     *            The toast duration
     */
    public static void toast(Context context, String msg, int duration) {
        Toast toast = getToast(context, msg, duration);
        toast.show();
    }

    /**
     * return a toast object for toast
     *
     * @param context
     * @param msg
     * @param duration
     * @return the Toast singleton
     */
    private static Toast getToast(Context context, String msg, int duration) {
        if (sToast == null) {
            sToast = Toast.makeText(context.getApplicationContext(), DEFAULT_TOAST_STRING,
                    duration);
        }
        sToast.setText(msg);
        sToast.setDuration(duration);
        return sToast;
    }

    public static void toastFromNoneUiThread(String text, int duration) {
        Log.i(TAG, "toastFromNoneUiThread " + text);
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "show toast " + text);
                Toast.makeText(ApplicationUtils.getApplicationInstance(),
                        text, duration).show();
            }
        });
    }

    public static void toastFromNoneUiThread(final int msgResId) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(ApplicationUtils.getApplicationInstance(),
                        msgResId, Toast.LENGTH_SHORT).show();
            }
        });
    }
}


评论