Android应用总是会不可避免的发生crash,也就是崩溃。Android提供了处理这类问题的方法,当crash发生的时候,系统就会回调UncaughtExceptionHandler的uncaughtException方法。在uncaughtException方法中就可以获取到异常信息,可以选择将异常信息存储到sd卡,然后在合适的时候将其上传到服务器。
实现方法:
<1> 创建一个类CrashHanlder继承UnCaughtExceptionHandler,实现里面的uncaughtException方法
public class CrashHandler implements Thread.UncaughtExceptionHandler {
private static CrashHandler crashHandler;
private Context context;
private Thread.UncaughtExceptionHandler mDefaultCrashHandler;
private static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/crash/log/";
private static final String FILE_NAME = "crash";
private static final String FILE_NAME_SUFFIX = ".trace";
public static CrashHandler getInstance(){
if(crashHandler == null){
crashHandler = new CrashHandler();
}
return crashHandler;
}
public void init(Context context){
context = context.getApplicationContext();
//设置系统默认异常处理器
Thread.setDefaultUncaughtExceptionHandler(this);
mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread t, Throwable e) {
try {
//将异常信息导出到SD卡中
dumpExceptionToSDCard(e);
// //上传异常信息到服务器,以便开发人员分析
// uploadExceptionToServer();
}catch (Exception e1){
e1.printStackTrace();
}
e.printStackTrace();
if(mDefaultCrashHandler != null){
mDefaultCrashHandler.uncaughtException(t,e);
}else{
Process.killProcess(Process.myPid());
}
}
private void dumpExceptionToSDCard(Throwable ex) throws Exception{
//判断SD卡是否存在或能否使用,不存在或无法使用就返回
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
return;
}
File dir = new File(PATH);
if(!dir.exists()){
dir.mkdirs();
}
long current = System.currentTimeMillis();
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(current));
File file = new File(PATH + FILE_NAME + time + FILE_NAME_SUFFIX);
if(!file.exists()){
file.createNewFile();
}
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
pw.println(time);
dumpPhoneInfo(pw);
pw.println();
ex.printStackTrace(pw);
}
private void dumpPhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(),PackageManager.GET_ACTIVITIES);
pw.println("APP Version");
pw.println(pi.versionName);
pw.println("_");
pw.println(pi.versionCode);
//Android版本号
pw.println("OS Version: ");
pw.println(Build.VERSION.RELEASE);
pw.println("_");
pw.println(Build.VERSION.SDK_INT);
//手机制造商
pw.println("Vendor:");
pw.println(Build.MANUFACTURER);
//手机号
pw.println("Model:");
pw.println(Build.MODEL);
//CPU架构
pw.println("CPU ABI:");
pw.println(Build.CPU_ABI);
}
}
<2> 在Application的onCreate方法中调用
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(this);