Global exception capture, and some small knowledge points

There is a very powerful operation here, which is to accurately capture the crash in the program, then print it, and store it on the mobile phone. If necessary, it can also be uploaded to the server, so that after our program is online, we can also know what kind of problems our program has.

code above

public class CrashHandler implements Thread.UncaughtExceptionHandler {
    //文件夹目录
    private static final String PATH = "/sdcard/crash_log";
    //文件名
    private static final String FILE_NAME = "crash";
    //文件名后缀
    public static final String FILE_NAME_SUFFIX = ".trace";
    //上下文
    private Context mContext;
    private static CrashHandler sInstance = new CrashHandler();
    public CrashHandler() {}
    public static CrashHandler getInstance() {
        return sInstance;
    }
    public void init(Context context) {
        //将当前实例设为系统默认的异常处理器
        Thread.setDefaultUncaughtExceptionHandler(this);
        //获取Context,方便内部使用
        mContext = context.getApplicationContext();
    }
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        //导出异常息到S信D卡
        dumpExceptionToSDCard(ex);
        //上传异常信息到服务器
        uploadExceptionToServer(ex);
        //延时1秒杀死进程
        SystemClock.sleep(2000);

        Process.killProcess(Process.myPid());
    }
    private void dumpExceptionToSDCard(Throwable ex) {
        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));
        //以当前时间创建log文件
        File file = new File(PATH + FILE_NAME + time + FILE_NAME_SUFFIX);
        try {
            //输出流操作
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            //导出手机信息和异常信息
            PackageManager pm = mContext.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
            pw.println("发生异常时间:" + time);
            pw.println("应用版本:" + pi.versionName);
            pw.println("应用版本号:" + pi.versionCode);
            pw.println("android版本号:" + Build.VERSION.RELEASE);
            pw.println("android版本号API:" + Build.VERSION.SDK_INT);
            pw.println("手机制造商:" + Build.MANUFACTURER);
            pw.println("手机型号:" + Build.MODEL);
            ex.printStackTrace(pw);
            //关闭输出流
            pw.close();
        } catch (Exception e) {

        }
    }
    private void uploadExceptionToServer(Throwable ex) {
        Error error = new Error(ex.getMessage());
        /*error.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
             }
        });*/
    }
}

Then register it globally

 public class BaseApplication extends AppData {


  @Override
    public void onCreate() {
        super.onCreate();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
    }




}

After this operation, if an exception occurs again, it can be automatically printed, as shown in the figure

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325624160&siteId=291194637