SingleToastUtil改为使用Toast框架
This commit is contained in:
@@ -93,6 +93,8 @@ dependencies {
|
||||
api "org.greenrobot:eventbus:${eventbusVersion}"
|
||||
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
|
||||
api 'com.github.getActivity:ToastUtils:9.6'
|
||||
|
||||
}
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
@@ -2,149 +2,50 @@ package com.yizhuan.xchat_android_library.utils;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.os.SystemClock;
|
||||
import android.text.TextUtils;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.yizhuan.xchat_android_library.utils.config.BasicConfig;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import com.hjq.toast.ToastUtils;
|
||||
|
||||
/**
|
||||
* Created by qinbo on 2014/8/12.
|
||||
*/
|
||||
public class SingleToastUtil {
|
||||
private static String oldMsg;
|
||||
protected static Toast toast = null;
|
||||
private static long oneTime = 0;
|
||||
private static long twoTime = 0;
|
||||
private static Handler handler;
|
||||
|
||||
public static void showToast(String s) {
|
||||
showToast(BasicConfig.INSTANCE.getAppContext(), s, Toast.LENGTH_LONG);
|
||||
ToastUtils.show(s);
|
||||
}
|
||||
|
||||
public static void showToast(int resId) {
|
||||
ToastUtils.show(resId);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void showToastShort(String s) {
|
||||
showToast(BasicConfig.INSTANCE.getAppContext(), s, Toast.LENGTH_SHORT);
|
||||
ToastUtils.show(s);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void showToastShort(int resId) {
|
||||
showToast(BasicConfig.INSTANCE.getAppContext(), BasicConfig.INSTANCE.getAppContext().getString(resId), Toast.LENGTH_SHORT);
|
||||
|
||||
ToastUtils.show(resId);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void showToast(Context context, String s) {
|
||||
showToast(context, s, Toast.LENGTH_LONG);
|
||||
}
|
||||
|
||||
private static void doShowToast(Context context, String s, int length) {
|
||||
try {
|
||||
if (toast == null) {
|
||||
toast = Toast.makeText(context, s, length);
|
||||
//尝试修复bugly上的一个大量崩溃
|
||||
//bugly地址:https://bugly.qq.com/v2/crash-reporting/crashes/52320483a6/910?pid=1
|
||||
//解决办法:https://blog.csdn.net/joye123/article/details/80738113
|
||||
hook(toast);
|
||||
toast.show();
|
||||
oneTime = SystemClock.uptimeMillis();
|
||||
} else {
|
||||
twoTime = SystemClock.uptimeMillis();
|
||||
if (s.equals(oldMsg)) {
|
||||
if (twoTime - oneTime > (length == 0 ? 2000 : 3500)) {
|
||||
toast.show(); // 频繁调用会出现无法展示的情况
|
||||
oneTime = twoTime;
|
||||
}
|
||||
} else {
|
||||
oldMsg = s;
|
||||
toast.setText(s);
|
||||
toast.show();
|
||||
oneTime = twoTime;
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
}
|
||||
ToastUtils.show(s);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void showToast(Context mContext, final String s, final int length) {
|
||||
if (TextUtils.isEmpty(s)) return;
|
||||
// 内存泄露的解决
|
||||
final Context context = BasicConfig.INSTANCE.getAppContext();
|
||||
if (Looper.myLooper() != Looper.getMainLooper()) {
|
||||
synchronized (SingleToastUtil.class) {
|
||||
if (null == handler) {
|
||||
handler = new Handler(Looper.getMainLooper());
|
||||
}
|
||||
}
|
||||
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
doShowToast(context, s, length);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
doShowToast(context, s, length);
|
||||
}
|
||||
ToastUtils.show(s);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void showToast(Context context, int resId, int length) {
|
||||
showToast(context, context.getString(resId), length);
|
||||
ToastUtils.show(resId);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void showToast(Context context, int resId) {
|
||||
showToast(context, resId, Toast.LENGTH_LONG);
|
||||
ToastUtils.show(resId);
|
||||
}
|
||||
|
||||
public static void replaceToast(Context context, String str) {
|
||||
if (toast == null) {
|
||||
showToast(context, str, Toast.LENGTH_LONG);
|
||||
} else {
|
||||
oldMsg = str;
|
||||
toast.setText(str);
|
||||
}
|
||||
}
|
||||
|
||||
public static class SafelyHandlerWarpper extends Handler {
|
||||
private Handler impl;
|
||||
|
||||
public SafelyHandlerWarpper(Handler impl) {
|
||||
this.impl = impl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispatchMessage(Message msg) {
|
||||
try {
|
||||
super.dispatchMessage(msg);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
impl.handleMessage(msg);//需要委托给原Handler执行
|
||||
}
|
||||
}
|
||||
|
||||
private static void hook(Toast toast) {
|
||||
int sdkInt = Build.VERSION.SDK_INT;
|
||||
if (sdkInt >= Build.VERSION_CODES.O) {
|
||||
//8.0或以上,此异常已经被处理 以下通过反射处理
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Field sField_TN = Toast.class.getDeclaredField("mTN");
|
||||
sField_TN.setAccessible(true);
|
||||
Field sField_TN_Handler = sField_TN.getType().getDeclaredField("mHandler");
|
||||
sField_TN_Handler.setAccessible(true);
|
||||
Object tn = sField_TN.get(toast);
|
||||
Handler preHandler = (Handler) sField_TN_Handler.get(tn);
|
||||
sField_TN_Handler.set(tn, new SafelyHandlerWarpper(preHandler));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user