赞
踩
mContext = context;mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();Thread.setDefaultUncaughtExceptionHandler(this);
if(!handleException(ex) && mDefaultHandler != null) {
mDefaultHandler.uncaughtException(thread, ex);
} else {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e(TAG, "error : ", e);
}android.os.Process.killProcess(android.os.Process.myPid());System.exit(0);
}
new Thread() {@Override
public void run() {Looper.prepare();Toast.makeText(mContext, "很抱歉,程序出现异常,即将退出.", Toast.LENGTH_LONG).show();Looper.loop();}
}.start();collectDeviceInfo(mContext);saveCrashInfo2File(ex);return true;
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);}
}
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 {
long timestamp = System.currentTimeMillis();String time = formatter.format(new Date());String fileName = "crash-" + time + "-" + timestamp + ".txt";String path = "/mnt/sdcard/musicCrash/";File dir = new File(path);if(!dir.exists()) {
dir.mkdirs();
}
if(dir.listFiles().length > 50) {
for(File file : dir.listFiles()) {
if(dir.listFiles().length > 50) {
file.delete();}
}
}FileOutputStream fos = new FileOutputStream(path + fileName);Log.e(TAG, new String(sb));fos.write(sb.toString().getBytes());fos.close();
return fileName;
} catch (Exception e) {
Log.e(TAG, "an error occured while writing file...", e);
}
return null;
}
@Overridepublic void onCreate() {
super.onCreate();Log.i(TAG, "application oncreate-----");
CrashHandler crashHandler = CrashHandler.getInstance();crashHandler.init(getApplicationContext());
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。