lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.kuanzhai.driver.utils;
 
import android.os.Looper;
import android.widget.Toast;
 
 
import com.kuanzhai.driver.base.MyApplication;
 
import java.util.HashMap;
import java.util.Map;
 
public class UniException implements Thread.UncaughtExceptionHandler {
 
    public static final String TAG = "UniException";
 
    // 系统默认的UncaughtException处理类
    private Thread.UncaughtExceptionHandler mDefaultHandler;
    // UniException实例
    private static UniException INSTANCE = new UniException();
 
    // 用来存储设备信息和异常信息
    private Map<String, String> infos = new HashMap<String, String>();
 
    /**
     * 保证只有一个实例
     */
    private UniException() {
    }
 
    /**
     * 获取UniException实例 ,单例模式
     */
    public static UniException getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new UniException();
            INSTANCE.init();
        }
        return INSTANCE;
    }
 
    /**
     * 初始化
     */
    public void init() {
        // 获取系统默认的UncaughtException处理器
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        // 设置该本类为程序的默认处理器
        Thread.setDefaultUncaughtExceptionHandler(this);
    }
 
    /**
     * 当UncaughtException发生时会转入该函数来处理
     */
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        if (!handleException(ex) && mDefaultHandler != null) {
            // 如果用户没有处理则让系统默认的异常处理器来处理
            mDefaultHandler.uncaughtException(thread, ex);
        } else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                LogUtils.e("error for app: ", e.getMessage());
            }
            // 退出程序
            ActivityCollector.getActivityCollector().AppExit();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(1);
        }
    }
 
    /**
     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
     *
     * @param ex
     * @return true:如果处理了该异常信息;否则返回false.
     */
    private boolean handleException(Throwable ex) {
        if (ex == null) {
            return false;
        }
        // 使用Toast来显示异常信息
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(MyApplication.Companion.getInstance(), "抱歉,程序出现异常", Toast.LENGTH_LONG).show();
 
                Looper.loop();
            }
        }.start();
//        // 收集设备参数信息
//        collectDeviceInfo(AppOS.appOS);
//        // 保存日志文件
//        try {
//            File file = new File(INI.INI_BUS_LOG_FLODER);
//            if (!file.exists()) {
//                file.mkdirs();
//            }
//            FileOutputStream fout;
//            fout = new FileOutputStream(new File(INI.INI_BUS_LOG_FLODER + DateUtil.getYourTime("yyyyMMdd") + ".log"),
//                    true);
//            fout.write((">>>>时间:" + DateUtil.getYourTime(DateUtil.yyyyMMdd_HH_mm_ss) + "\r\n").getBytes("utf-8"));
//            fout.write(("信息:" + ex.getMessage() + "\r\n").getBytes("utf-8"));
//            for (int i = 0; i < ex.getStackTrace().length; i++) {
//                fout.write(("****StackTrace" + i + "\r\n").getBytes("utf-8"));
//                fout.write(("行数:" + ex.getStackTrace()[i].getLineNumber() + "\r\n").getBytes("utf-8"));
//                fout.write(("类名:" + ex.getStackTrace()[i].getClassName() + "\r\n").getBytes("utf-8"));
//                fout.write(("文件:" + ex.getStackTrace()[i].getFileName() + "\r\n").getBytes("utf-8"));
//                fout.write(("方法:" + ex.getStackTrace()[i].getMethodName() + "\r\n\r\n").getBytes("utf-8"));
//            }
//            fout.write(
//                    "--------------------------------\r\n--------------------------------\r\n--------------------------------\r\n"
//                            .getBytes("utf-8"));
//            fout.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return true;
    }
}
 
//    /**
//     * 收集设备参数信息
//     *
//     * @param ctx
//     */
//    public void collectDeviceInfo(Context ctx) {
//        try {
//            PackageManager pm = ctx.getPackageManager();
//            PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES);
//            if (pi != null) {
//                String versionName = pi.versionName == null ? "null" : pi.versionName;
//                String versionCode = pi.versionCode + "";
//                infos.put("versionName", versionName);
//                infos.put("versionCode", versionCode);
//            }
//        } catch (NameNotFoundException e) {
//            LogUtil.e("an error occured when collect package info" + e.getMessage());
//        }
//        Field[] fields = Build.class.getDeclaredFields();
//        for (Field field : fields) {
//            try {
//                field.setAccessible(true);
//                infos.put(field.getName(), field.get(null).toString());
//                LogUtil.e(field.getName() + " : " + field.get(null));
//            } catch (Exception e) {
//                LogUtil.e("an error occured when collect crash info" + e.getMessage());
//            }
//        }
//    }