工具类一:解决子线程弹吐司崩溃问题
package com.example.grenaderose.redthunder.utils;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
public class ToastUtils {
//使用主线程looper初始化handler,保证handler发送的消息运行在主线程
private static Handler handler = new Handler(Looper.getMainLooper());
public static void show(final Context ctx, final String text) {
//判断当前是主线程还是子线程
if (Looper.myLooper() == Looper.getMainLooper()) {
//当前looper是否等于主线程looper, 如果是, 说明当前是在主线程
Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show();
//System.out.println("主线程吐司....");
} else {
//子线程
//handler.sendEmptyMessage(0);//handler发送一个消息给队列
//handler发送一个任务给队列
handler.post(new Runnable() {
@Override
public void run() {
//当Looper轮询到此任务时, 会在主线程运行此方法
Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show();
}
});
//System.out.println("子线程吐司....");
}
}
}
工具类二:解决重复弹吐司问题
public class ToastUtil {
private static String oldMsg;
protected static Toast toast = null;
private static long oneTime = 0;
private static long twoTime = 0;
public static void showToast(Context context, String s){
if(toast == null){
toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
toast.show();
oneTime = System.currentTimeMillis();
}else{
twoTime = System.currentTimeMillis();
if(s.equals(oldMsg)){
if(twoTime - oneTime > Toast.LENGTH_SHORT){ //20230627 Toast.LENGTH_SHORT 为0,这一直为true,没卵用的判断
toast.show();
}
}else{
oldMsg = s;
toast.setText(s);
toast.show();
}
}
oneTime = twoTime;
}
public static void showToast(Context context, int resId){
showToast(context, context.getString(resId));
}
}
工具类三:把他俩结合一下把,省事,直接调用show()
public class ToastUtil {
private static String oldMsg;
protected static Toast toast = null;
private static long oneTime = 0;
private static long twoTime = 0;
//使用主线程looper初始化handler,保证handler发送的消息运行在主线程
private static Handler handler = new Handler(Looper.getMainLooper());
public static void show(final Context ctx, final String text) {
//判断当前是主线程还是子线程
if (Looper.myLooper() == Looper.getMainLooper()) {
//当前looper是否等于主线程looper, 如果是, 说明当前是在主线程
// Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show();
//System.out.println("主线程吐司....");
showToast(ctx,text);
} else {
//子线程
//handler.sendEmptyMessage(0);//handler发送一个消息给队列
//handler发送一个任务给队列
handler.post(new Runnable() {
@Override
public void run() {
//当Looper轮询到此任务时, 会在主线程运行此方法
// Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show();
showToast(ctx,text);
}
});
//System.out.println("子线程吐司....");
}
}
//coper 2023/7/13 子线程弹toast会崩溃
public static void showToast(Context context, String s){
if(toast == null){
toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
toast.show();
oneTime = System.currentTimeMillis();
}else{
twoTime = System.currentTimeMillis();
if(s.equals(oldMsg)){
if(twoTime - oneTime > Toast.LENGTH_SHORT){ //20230627 Toast.LENGTH_SHORT 为0,这一直为true,没卵用的判断
toast.show();
}
}else{
oldMsg = s;
toast.setText(s);
toast.show();
}
}
oneTime = twoTime;
}
public static void showToast(Context context, int resId){
showToast(context, context.getString(resId));
}
}