package com.gh.base; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Looper; import android.preference.PreferenceManager; import android.util.Log; import com.gh.common.constant.Config; import com.gh.common.util.DataCollectionUtils; import com.gh.common.util.DataUtils; import com.gh.gamecenter.SplashScreenActivity; import com.lightgame.config.CommonDebug; import com.lightgame.download.FileUtils; import com.lightgame.utils.AppManager; import com.lightgame.utils.Utils; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.lang.Thread.UncaughtExceptionHandler; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.concurrent.TimeoutException; import io.sentry.Sentry; public class AppUncaughtHandler implements UncaughtExceptionHandler { private Context mContext; public AppUncaughtHandler(Context context) { // 获取系统默认的UncaughtException处理器 mContext = context; } @Override public void uncaughtException(Thread t, Throwable e) { if (("FinalizerWatchdogDaemon").equals(t.getName()) && e instanceof TimeoutException) { // ignore timeoutException // detail can be found in this didi tech blog post https://mp.weixin.qq.com/s?__biz=MzU1ODEzNjI2NA==&mid=2247487185&idx=2&sn=cf2d9e10053f625bde0f61d246f14870&source=41#wechat_redirect } else { new Thread(() -> { Looper.prepare(); Utils.toast(mContext.getApplicationContext(), "\"光环助手\"发生错误"); Looper.loop(); }); saveLocalLog(mContext, e); restart(mContext); Sentry.captureException(e); } } public static void restart(final Context context) { // S450 这机器会无限重启循环 : ( if ("S450".equals(Build.MODEL)) return; // 防止重复奔溃,导致助手一直重启,20秒内不做处理 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); long curTime = System.currentTimeMillis(); long time = sp.getLong("last_restart_time", 0); if (curTime - time > 20 * 1000) { sp.edit().putLong("last_restart_time", curTime).apply(); Intent intent = new Intent(context, SplashScreenActivity.class); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // 退出程序并重启 AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, curTime + 3000, restartIntent); // 1秒钟后重启应用 } //error restart // System.exit(2); AppManager.getInstance().finishAllActivity(); } // 保存log到本地 public static void saveLocalLog(Context context, Throwable ex) { String errorMsg = Log.getStackTraceString(ex); Config.setExceptionMsg(errorMsg); // 保存到本地 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.getDefault()); File file = new File(FileUtils.getLogPath(context.getApplicationContext(), format.format(new Date()) + "_gh_assist" + ".log")); FileWriter writer = null; try { file.createNewFile(); writer = new FileWriter(file); writer.write(errorMsg); writer.flush(); } catch (IOException e1) { e1.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 下次应用启动再上报 * * @param context * @param throwable */ public static void reportException(Context context, Throwable throwable) { CommonDebug.logMethodWithParams(context, "ERRMSG", throwable); // 上传错误数据 try { DataCollectionUtils.uploadError(context, Log.getStackTraceString(throwable)); } catch (Exception e) { } DataUtils.onError(context, throwable); } }