全局crash捕捉并上传的服务器

全局crash捕捉并上传的服务器

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
public class CrashHandler implements UncaughtExceptionHandler {
public static final String TAG = "CrashHandler";
public static final boolean DEBUG = false;
public static String PATH;
private static final String FILE_NAME = "crash";
private static final String FILE_NAME_SUFFIX = ".log";
private static CrashHandler sInstance = new CrashHandler();
private UncaughtExceptionHandler mDefaultCrashHandler;
private Context mContext;
private HttpUtils httpUtils;
private CrashHandler() {
}
public static CrashHandler getInstance() {
return sInstance;
}
public void init(Context context) {
httpUtils = new HttpUtils();
mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
mContext = context.getApplicationContext();
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
try {
boolean sdCardExist = Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist) {
PATH = mContext.getExternalCacheDir().getAbsolutePath() + File.separator + "CrashText" + File.separator + "log" + File.separator;
} else {
PATH = mContext.getFilesDir().getAbsolutePath() + File.separator + "CrashText" + File.separator + "log" + File.separator;
}
//导出到sdCard AND 上传至服务器
dumpException2SDCARD(ex);
ex.printStackTrace();
if (!handleException(ex) && mDefaultCrashHandler != null) {
mDefaultCrashHandler.uncaughtException(thread, ex);
} else {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e(TAG, "error : ", e);
}
BaseActivity.exit();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean handleException(Throwable ex) throws IOException {
if (ex == null) {
return false;
}
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(mContext, "很抱歉,程序出现异常,即将退出.", Toast.LENGTH_SHORT).show();
Looper.loop();
}
}).start();
return true;
}
private void dumpException2SDCARD(Throwable ex) throws IOException {
long current = System.currentTimeMillis();
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(current));
File file = new File(PATH);
if (!file.exists()) {
file.mkdirs();
}
File file1 = new File(PATH + FILE_NAME + time + FILE_NAME + FILE_NAME_SUFFIX);
if (!file1.exists()) {
file1.createNewFile();
}
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file1)));
pw.println(time);
dumpPhoneInfo(pw);
pw.println();
ex.printStackTrace(pw);
pw.close();
RequestParams params = new RequestParams();
params.addBodyParameter("file", file1);
httpUtils.send(HttpRequest.HttpMethod.POST, Constant.UPLOADLOG, params,
new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
Log.i("tag", "======success");
}
@Override
public void onFailure(HttpException error, String msg) {
}
});
Log.i(TAG, "===========" + file1.getAbsolutePath());
} catch (Exception e) {
Log.e(TAG, "写入失败");
}
}
private void dumpPhoneInfo(PrintWriter pw) throws NameNotFoundException {
PackageManager pm = mContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
pw.print("APP Version:");
pw.print(pi.versionName);
pw.print("_");
pw.println(pi.versionCode);
pw.print("Android Version:");
pw.print(Build.VERSION.RELEASE);
pw.print("_");
pw.println(Build.VERSION.SDK_INT);
pw.print("Vendor:");
pw.println(Build.MANUFACTURER);
pw.print("Model:");
pw.println(Build.MODEL);
pw.print("CPU ABI");
pw.println(Build.CPU_ABI);
}
}

然后在application里初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Application extends android.app.Application {
private static Application sInstance;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(this);
}
public static Application getInstance() {
return sInstance;
}
}