Android-记录崩溃日志
2016-11-17 20:19
176 查看
public class CrashHanlderExcetpion implements UncaughtExceptionHandler { public static final String TAG = CrashHanlderExcetpion.class.getSimpleName(); private Context mContext; private UncaughtExceptionHandler mDefaultHandler; private Map<String, String> infos = new HashMap<String, String>(); private static CrashHanlderExcetpion INSTANCE = new CrashHanlderExcetpion(); private CrashHanlderExcetpion() { } public static CrashHanlderExcetpion getInstance() { return INSTANCE; } public void init(Context context) { mContext = context; mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(this); } @Override public void uncaughtException(Thread thread, Throwable ex) { if (!handleException(ex) && mDefaultHandler != null) { mDefaultHandler.uncaughtException(thread, ex); } else { android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); } } private boolean handleException(Throwable ex) { if (ex == null) { return false; } new Thread() { @Override public void run() { Looper.prepare(); Toast.makeText(mContext, "抱歉,出现异常,即将退出", Toast.LENGTH_SHORT).show(); Looper.loop(); } }.start(); collectDeviceInfo(mContext); dumpCrashInfo2File(ex, "crash"); dumpHprof2File(ex, "crash"); return true; } 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); } } } private void dumpCrashInfo2File(Throwable ex, String dir) { StringBuffer sb = new StringBuffer(); for (Map.Entry<String, String> entry : infos.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); sb.append(key + "=" + value + "\n"); } 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 { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+dir; File fileDir = new File(path); if (!fileDir.exists()) { fileDir.mkdirs(); } File crashFile = new File(fileDir, "crash-" + getTimeString() + ".txt"); FileOutputStream fos = new FileOutputStream(crashFile); fos.write(sb.toString().getBytes()); fos.close(); } } catch (Exception e) { Log.e(TAG, "an error occured while writing file...", e); } } private void dumpHprof2File(Throwable ex, String dir) { if (null == ex) return; Throwable thr = ex; while (null != thr) { String cls = thr.getClass().getName(); if (null != cls && cls.contains("OutOfMemoryError")) { String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+dir; File fileDir = new File(path); if (!fileDir.exists()){ fileDir.mkdirs(); } File hprofFile = new File(fileDir, "hprof-" + getTimeString() + ".hprof"); try { Debug.dumpHprofData(hprofFile.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } break; } thr = thr.getCause(); } } private String getTimeString(){ Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); return simpleDateFormat.format(date); } }
在Application启动的时候调用一下:
CrashHanlderExcetpion.getInstance().init(this);
相关文章推荐
- Android全局捕获崩溃异常记录日志保存至本地并定时删除
- Android中处理崩溃异常和记录日志
- Android中处理崩溃异常和记录日志
- Android中处理崩溃异常和记录日志
- Android中处理崩溃异常和记录日志
- Android中处理崩溃异常和记录日志
- Android全局捕获崩溃异常记录日志保存至本地并定时删除
- Android中处理崩溃异常和记录日志
- Android中处理崩溃异常和记录日志(转)
- Android崩溃日志记录
- Android全局捕获崩溃异常记录日志保存至本地并定时删除
- Android 全局崩溃日志(CrashHandle)记录
- Android 中处理崩溃异常和记录日志
- Android中处理崩溃异常和记录日志
- android 记录崩溃日志
- [置顶] android 底层日志 开发框架 崩溃后进行记录 LogReport
- android 记录崩溃日志
- Android全局捕获崩溃异常记录日志保存至本地并定时删除
- Android开发之处理崩溃异常和记录日志
- 捕获android程序崩溃日志