package com.uiui.os.utils; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Color; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.util.Log; import android.widget.Toast; import com.blankj.utilcode.util.ToastUtils; import com.uiui.os.BuildConfig; /** * Created by haoge on 2017/3/2. */ public class ToastUtil { private static final String TAG = ToastUtil.class.getSimpleName(); @SuppressLint("StaticFieldLeak") private static Context mContext; private static Handler mainHandler = new Handler(Looper.getMainLooper()); private static Toast debugToast; private static Toast toast; @SuppressLint("ShowToast") public static void init(Context context) { mContext = context; toast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT); debugToast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT); } private static long time1 = 0L; private static long time2 = 0L; public static void show(final String msg) { ToastUtils.make() // .setBgColor(ColorUtils.getColor(R.color.toast_color)) .setTextColor(Color.DKGRAY) // .setGravity(Gravity.CENTER, 0, 0) .setNotUseSystemToast() .show(msg); } public static void betaShow(final String msg) { if ( BuildConfig.DEBUG) { ToastUtils.make() // .setBgColor(ColorUtils.getColor(R.color.toast_color)) .setTextColor(Color.RED) // .setGravity(Gravity.CENTER, 0, 0) .setNotUseSystemToast() .setDurationIsLong(true) .show(msg); } else { Log.e(TAG, "debugShow: " + msg); } } private static Toast mToast = null; //android 8.0以后限制 //https://www.jianshu.com/p/d9813ad03d59 //https://www.jianshu.com/p/050ce052b873 public static void showToast(Context context, String text, int duration) { if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) { Toast.makeText(context, text, duration).show(); } else { if (mToast == null) { mToast = Toast.makeText(context, text, duration); } else { mToast.setText(text); mToast.setDuration(duration); } mToast.show(); } } // public static void showInCenter(String msg) { // mainHandler.post(() -> { // if (toast != null) { // toast.setGravity(Gravity.CENTER, 0, 0); // toast.setText(msg); // toast.show(); // } // }); // } }