android 程序漰溃 后台handle处理类
[代码] [Java]代码
002 |
import java.io.FileOutputStream; |
003 |
import java.io.FilenameFilter; |
004 |
import java.io.PrintWriter; |
005 |
import java.io.StringWriter; |
006 |
import java.io.Writer; |
007 |
import java.lang.Thread.UncaughtExceptionHandler; |
008 |
import java.lang.reflect.Field; |
009 |
import java.util.Arrays; |
010 |
import java.util.Properties; |
011 |
import java.util.TreeSet; |
013 |
import android.content.Context; |
014 |
import android.content.pm.PackageInfo; |
015 |
import android.content.pm.PackageManager; |
016 |
import android.content.pm.PackageManager.NameNotFoundException; |
017 |
import android.os.Build; |
018 |
import android.os.Looper; |
019 |
import android.util.Log; |
020 |
import android.widget.Toast; |
023 |
* UncaughtException处理类,当程序发生Uncaught异常的时候,有该类 来接管程序,并记录 发送错误报告.. 註冊方式 |
024 |
* CrashHandler crashHandler = CrashHandler.getInstance(); //注册crashHandler |
025 |
* crashHandler.init(getApplicationContext()); //发送以前没发送的报告(可选) |
026 |
* crashHandler.sendPreviousReportsToServer(); |
029 |
public class CrashHandler implements UncaughtExceptionHandler { |
031 |
public static final String TAG = "CrashHandler" ; |
033 |
* 是否开启日志输出,在Debug状态下开启, 在Release状态下关闭以提示程序性能 |
035 |
public static final boolean DEBUG = true ; |
036 |
/** 系统默认的UncaughtException处理类 */ |
037 |
private Thread.UncaughtExceptionHandler mDefaultHandler; |
038 |
/** CrashHandler实例 */ |
039 |
private static CrashHandler INSTANCE; |
041 |
private Context mContext; |
043 |
/** 使用Properties来保存设备的信息和错误堆栈信息 */ |
044 |
private Properties mDeviceCrashInfo = new Properties(); |
045 |
private static final String VERSION_NAME = "versionName" ; |
046 |
private static final String VERSION_CODE = "versionCode" ; |
047 |
private static final String STACK_TRACE = "STACK_TRACE" ; |
049 |
private static final String CRASH_REPORTER_EXTENSION = ".cr" ; |
051 |
/** 保证只有一个CrashHandler实例 */ |
052 |
private CrashHandler() { |
055 |
/** 获取CrashHandler实例 ,单例模式 */ |
056 |
public static CrashHandler getInstance() { |
057 |
if (INSTANCE == null ) { |
058 |
INSTANCE = new CrashHandler(); |
064 |
* 初始化,注册Context对象, 获取系统默认的UncaughtException处理器, 设置该CrashHandler为程序的默认处理器 |
068 |
public void init(Context ctx) { |
070 |
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler(); |
071 |
Thread.setDefaultUncaughtExceptionHandler( this ); |
075 |
* 当UncaughtException发生时会转入该函数来处理 |
078 |
public void uncaughtException(Thread thread, Throwable ex) { |
079 |
if (!handleException(ex) && mDefaultHandler != null ) { |
081 |
mDefaultHandler.uncaughtException(thread, ex); |
086 |
} catch (InterruptedException e) { |
087 |
Log.e(TAG, "Error : " , e); |
089 |
android.os.Process.killProcess(android.os.Process.myPid()); |
095 |
* 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. 开发者可以根据自己的情况来自定义异常处理逻辑 |
098 |
* @return true:如果处理了该异常信息;否则返回false |
100 |
private boolean handleException(Throwable ex) { |
104 |
final String msg = ex.getLocalizedMessage(); |
110 |
Toast.makeText(mContext, "程序出错啦:" + msg, Toast.LENGTH_LONG) |
117 |
collectCrashDeviceInfo(mContext); |
119 |
String crashFileName = saveCrashInfoToFile(ex); |
121 |
sendCrashReportsToServer(mContext); |
126 |
* 在程序启动时候, 可以调用该函数来发送以前没有发送的报告 |
128 |
public void sendPreviousReportsToServer() { |
129 |
sendCrashReportsToServer(mContext); |
133 |
* 把错误报告发送给服务器,包含新产生的和以前没发送的. |
137 |
private void sendCrashReportsToServer(Context ctx) { |
138 |
String[] crFiles = getCrashReportFiles(ctx); |
139 |
if (crFiles != null && crFiles.length > 0 ) { |
140 |
TreeSet<String> sortedFiles = new TreeSet<String>(); |
141 |
sortedFiles.addAll(Arrays.asList(crFiles)); |
143 |
for (String fileName : sortedFiles) { |
144 |
File cr = new File(ctx.getFilesDir(), fileName); |
151 |
private void postReport(File file) { |
163 |
private String[] getCrashReportFiles(Context ctx) { |
164 |
File filesDir = ctx.getFilesDir(); |
165 |
FilenameFilter filter = new FilenameFilter() { |
166 |
public boolean accept(File dir, String name) { |
167 |
return name.endsWith(CRASH_REPORTER_EXTENSION); |
170 |
return filesDir.list(filter); |
179 |
private String saveCrashInfoToFile(Throwable ex) { |
180 |
Writer info = new StringWriter(); |
181 |
PrintWriter printWriter = new PrintWriter(info); |
182 |
ex.printStackTrace(printWriter); |
184 |
Throwable cause = ex.getCause(); |
185 |
while (cause != null ) { |
186 |
cause.printStackTrace(printWriter); |
187 |
cause = cause.getCause(); |
190 |
String result = info.toString(); |
192 |
mDeviceCrashInfo.put(STACK_TRACE, result); |
193 |
String fileName = "" ; |
195 |
long timestamp = System.currentTimeMillis(); |
196 |
fileName = "crash-" + timestamp + CRASH_REPORTER_EXTENSION; |
197 |
FileOutputStream trace = mContext.openFileOutput(fileName, |
198 |
Context.MODE_PRIVATE); |
199 |
mDeviceCrashInfo.store(trace, "" ); |
203 |
} catch (Exception e) { |
204 |
Log.e(TAG, "an error occured while writing report file..." |
215 |
public void collectCrashDeviceInfo(Context ctx) { |
217 |
PackageManager pm = ctx.getPackageManager(); |
218 |
PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), |
219 |
PackageManager.GET_ACTIVITIES); |
221 |
mDeviceCrashInfo.put(VERSION_NAME, |
222 |
pi.versionName == null ? "not set" : pi.versionName); |
223 |
mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode); |
225 |
} catch (NameNotFoundException e) { |
226 |
Log.e(TAG, "Error while collect package info" , e); |
231 |
Field[] fields = Build. class .getDeclaredFields(); |
232 |
for (Field field : fields) { |
234 |
field.setAccessible( true ); |
235 |
mDeviceCrashInfo.put(field.getName(), field.get( null )); |
237 |
Log.d(TAG, field.getName() + " : " + field.get( null )); |
239 |
} catch (Exception e) { |
240 |
Log.e(TAG, "Error while collect crash info" , e); |
一起学习GIS及其二次开发,一起进步!
原文地址:https://www.cnblogs.com/tuncaysanli/p/2469042.html