Android程序崩溃捕获

概述

何为Unchecked异常呢,换句话说就是指非受检异常,它不能用try-catch来显示捕捉。

在Java中所有不是RuntimeException派生的Exception都是检查型异常。当函数中存在抛出检查型异常的操作时该函数的函数声明中必须包含throws语句。调用改函数的函数也必须对该异常进行处理,如不进行处理则必须在调用函数上声明throws语句。

检查型异常是JAVA首创的,在编译期对异常的处理有强制性的要求。在JDK代码中大量的异常属于检查型异常,包括IOException,SQLException等等。

在Java中所有RuntimeException的派生类都是非检查型异常,与检查型异常对比,非检查型异常可以不在函数声明中添加throws语句,调用函数上也不需要强制处理。
常见的NullPointException,ClassCastException是常见的非检查型异常。非检查型异常可以不使用try…catch进行处理,但是如果有异常产生,则异常将由JVM进行处理。对于RuntimeException的子类最好也使用异常处理机制。虽然RuntimeException的异常可以不使用try…catch进行处理,但是如果一旦发生异常,则肯定会导致程序中断执行,所以,为了保证程序再出错后依然可以执行,在开发代码时最好使用try…catch的异常处理机制进行处理。

使用UncaughtExceptionHandler来捕获unchecked异常

UncaughtException处理类,当程序发生Uncaught异常的时候,由该类来接管程序,并记录发送错误报告。

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

/**
* UncaughtException处理类,当程序发生Uncaught异常的时候,有该类来接管程序,并记录发送错误报告.
*
* @author user
*
*/
@SuppressLint("SdCardPath")
public class CrashHandler implements UncaughtExceptionHandler {

public static final String TAG = "TEST";

// CrashHandler 实例
private static CrashHandler INSTANCE = new CrashHandler();

// 程序的 Context 对象
private Context mContext;

// 系统默认的 UncaughtException 处理类
private Thread.UncaughtExceptionHandler mDefaultHandler;

// 用来存储设备信息和异常信息
private Map<String, String> infos = new HashMap<String, String>();

// 用来显示Toast中的信息
private static String error = "程序错误,额,不对,我应该说,服务器正在维护中,请稍后再试";

private static final Map<String, String> regexMap = new HashMap<String, String>();

// 用于格式化日期,作为日志文件名的一部分
private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss",
Locale.CHINA);

/** 保证只有一个 CrashHandler 实例 */
private CrashHandler() {
//
}

/** 获取 CrashHandler 实例 ,单例模式 */
public static CrashHandler getInstance() {
initMap();
return INSTANCE;
}

/**
* 初始化
*
* @param context
*/
public void init(Context context) {
mContext = context;

// 获取系统默认的 UncaughtException 处理器
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();

// 设置该 CrashHandler 为程序的默认处理器
Thread.setDefaultUncaughtExceptionHandler(this);
Log.d("TEST", "Crash:init");
}

/**
* 当 UncaughtException 发生时会转入该函数来处理
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mDefaultHandler != null) {
// 如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
Log.d("TEST", "defalut");
} else {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e(TAG, "error : ", e);
}
// 退出程序
android.os.Process.killProcess(android.os.Process.myPid());
// mDefaultHandler.uncaughtException(thread, ex);
System.exit(1);
}
}

/**
* 自定义错误处理,收集错误信息,发送错误报告等操作均在此完成
*
* @param ex
* @return true:如果处理了该异常信息;否则返回 false
*/
private boolean handleException(Throwable ex) {
if (ex == null) {
return false;
}

// 收集设备参数信息
// collectDeviceInfo(mContext);
// 保存日志文件
saveCrashInfo2File(ex);
// 使用 Toast 来显示异常信息
new Thread() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(mContext, error, Toast.LENGTH_LONG).show();
Looper.loop();
}
}.start();
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) {
Log.e(TAG, "an error occured when collect package info", e);
}

Field[] fields = Build.class.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
infos.put(field.getName(), field.get(null).toString());
Log.d(TAG, field.getName() + " : " + field.get(null));
} catch (Exception e) {
Log.e(TAG, "an error occured when collect crash info", e);
}
}
}

/**
* 保存错误信息到文件中 *
*
* @param ex
* @return 返回文件名称,便于将文件传送到服务器
*/
private String saveCrashInfo2File(Throwable ex) {
StringBuffer sb = getTraceInfo(ex);
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
ex.printStackTrace(printWriter);
Throwable cause = ex.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();

String result = writer.toString();
sb.append(result);
try {
long timestamp = System.currentTimeMillis();
String time = formatter.format(new Date());
String fileName = "crash-" + time + "-" + timestamp + ".log";

if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String path = Environment.getExternalStorageDirectory()
+ "/crash/";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path + fileName);
fos.write(sb.toString().getBytes());
fos.close();
}

return fileName;
} catch (Exception e) {
Log.e(TAG, "an error occured while writing file...", e);
}

return null;
}

/**
* 整理异常信息
* @param e
* @return
*/
public static StringBuffer getTraceInfo(Throwable e) {
StringBuffer sb = new StringBuffer();

Throwable ex = e.getCause() == null ? e : e.getCause();
StackTraceElement[] stacks = ex.getStackTrace();
for (int i = 0; i < stacks.length; i++) {
if (i == 0) {
setError(ex.toString());
}
sb.append("class: ").append(stacks[i].getClassName())
.append("; method: ").append(stacks[i].getMethodName())
.append("; line: ").append(stacks[i].getLineNumber())
.append("; Exception: ").append(ex.toString() + "\n");
}
Log.d(TAG, sb.toString());
return sb;
}

/**
* 设置错误的提示语
* @param e
*/
public static void setError(String e) {
Pattern pattern;
Matcher matcher;
for (Entry<String, String> m : regexMap.entrySet()) {
Log.d(TAG, e+"key:" + m.getKey() + "; value:" + m.getValue());
pattern = Pattern.compile(m.getKey());
matcher = pattern.matcher(e);
if(matcher.matches()){
error = m.getValue();
break;
}
}
}

/**
* 初始化错误的提示语
*/
private static void initMap() {
// Java.lang.NullPointerException
// java.lang.ClassNotFoundException
// java.lang.ArithmeticException
// java.lang.ArrayIndexOutOfBoundsException
// java.lang.IllegalArgumentException
// java.lang.IllegalAccessException
// SecturityException
// NumberFormatException
// OutOfMemoryError
// StackOverflowError
// RuntimeException
regexMap.put(".*NullPointerException.*", "嘿,无中生有~Boom!");
regexMap.put(".*ClassNotFoundException.*", "你确定你能找得到它?");
regexMap.put(".*ArithmeticException.*", "我猜你的数学是体育老师教的,对吧?");
regexMap.put(".*ArrayIndexOutOfBoundsException.*", "恩,无下限=无节操,请不要跟我搭话");
regexMap.put(".*IllegalArgumentException.*", "你的出生就是一场错误。");
regexMap.put(".*IllegalAccessException.*", "很遗憾,你的信用卡账号被冻结了,无权支付");
regexMap.put(".*SecturityException.*", "死神马上降临");
regexMap.put(".*NumberFormatException.*", "想要改变一下自己形象?去泰国吧,包你满意");
regexMap.put(".*OutOfMemoryError.*", "或许你该减减肥了");
regexMap.put(".*StackOverflowError.*", "啊,啊,憋不住了!");
regexMap.put(".*RuntimeException.*", "你的人生走错了方向,重来吧");

}

}

在 Application 中初始化

1
2
3
4
5
6
7
8
9
10
public class CrashApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(getApplicationContext());
}

}

提交错误日志到网络服务器这一块还没有添加。如果添加了这一块功能,就能够实时的得要用户使用时的错误日志,能够及时反馈不同机型不同时候发生的错误,能对我们开发者的后期维护带来极大的方便。

崩溃的后续

收集完崩溃日志只是第一步,将日志收集分析才是目的。日志可以通过接口传回服务器,也可以通过接入第三方平台来收集。

友盟

友盟+支持移动应用统计和分析流量来源、内容使用、用户属性和行为数据,以便运维人员利用数据进行产品、运营、推广策略的决策。

腾讯Bugly

腾讯Bugly,为移动开发者提供专业的异常上报,运营统计和内测分发解决方案,帮助开发者快速发现并解决异常,同时掌握产品运营动态,及时跟进用户反馈。Bugly功能比较专注错误统计,提供APP上线崩溃分析、ANR分析、错误分析等等。

Firebase

海外版APP首选,Firebase能让你的App从零到一。也就是说它可以帮助手机以及网页应用的开发者轻松构建App。通过Firebase背后负载的框架就可以简单地开发一个App,无需服务器以及基础设施。

  • 无需管理基础架构,快速构建您的应用
  • Firebase SDK(如 Analytics、数据库、通知和崩溃报告)可让您快速迁移并专注于您的用户。
  • 由 Google 提供支持,受到众多热门应用的信赖
  • Firebase 建立在 Google 基础架构上,可以自动扩展,所以您不用担心是否能满足用户需求。
  • 一个控制台,其中各种产品配合使用
  • Firebase 产品可以独立工作,共享数据和分析结果,并且可从统一的信息中心进行访问。

GrowingIO

无痕埋点。GrowingIO 是基于用户行为的新一代数据分析产品,无需埋点即可采集全量、实时用户行为数据,数据分析更精细,帮助管理者、产品经理、市场运营、数据分析师、增长黑客等提升转化率、优化网站 / APP,实现用户快速增长和变现

Fabric

更好用的Bug收集平台.提供强大且轻便的APP Crash统计报告,帮助开发者快速定位Bug和Crash,其准确度远超过Bugly。提供APP beta自动化发布和部署平台。

文章作者: cpacm
文章链接: http://www.cpacm.net/2019/03/01/Android程序崩溃捕获/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 cpacm
打赏
  • 微信
  • 支付宝

评论