自定义全局异常类

自定义异常类


  • application (全局注册异常类)
package com.mrko.basic;

import android.app.Application;

import utils.MyException;


/**
 * Author:  Mrko
 * Date:    2017/8/17
 * Description:
 */
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        MyException.getInstance().init(this);
    }
}

  • MyException (异常类)
package utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * Author:  Mrko
 * Date:    2017/8/17
 * Description:
 */
public class MyException implements Thread.UncaughtExceptionHandler {
    private static final String CRASH = "crash";
    private static final String TAG = "MyException";
    private static final String CREAT_FILE_NAME = "creat_file_name";
    private Context mContext;
    private Thread.UncaughtExceptionHandler mDefultExceptionHanlder;
    private static MyException mInstance;

    private MyException() {

    }

    public static MyException getInstance() {
        if (mInstance == null) {
            synchronized (MyException.class) {
                if (mInstance == null) {
                    mInstance = new MyException();
                }
            }
        }
        return mInstance;
    }

    public void init(Context context) {
        this.mContext = context;
        Thread.currentThread().setUncaughtExceptionHandler(this);
        mDefultExceptionHanlder = Thread.currentThread().getUncaughtExceptionHandler();
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        Log.e(TAG, "捕获到异常了");
        mDefultExceptionHanlder.uncaughtException(t, e);
        //写入文件 1,手机信息。2,应用信息包名版本。 3,崩溃的详细信息。 4,保存当前文件
        String crashFileName = saveInfoToSD(e);
        Log.e(TAG, "FileName-->" + crashFileName);
        cacheCrashFile(crashFileName);

    }

    public void cacheCrashFile(String crashFileName) {
        SharedPreferences sp = mContext.getSharedPreferences(CRASH, Context.MODE_PRIVATE);
        sp.edit().putString(CREAT_FILE_NAME, crashFileName).commit();
    }

    public File getCrashFile() {
        String crashFileName = mContext.getSharedPreferences(CRASH, Context.MODE_APPEND).getString(CREAT_FILE_NAME, "");
        return new File(crashFileName);
    }

    /**
     * 保存到文件的信息
     *
     * @param e
     * @return
     */
    private String saveInfoToSD(Throwable e) {
        String fileName = null;
        StringBuffer sb = new StringBuffer();
        //1,应用信息包名版本 ,2,手机信息。
        for (Map.Entry entry : obtainSimpleInfo(mContext).entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            sb.append(key).append(" = ").append(value).append("\n");
        }

        //3,崩溃的详细信息。
        sb.append(obtainExceptioninfo(e));

        // 4,保存当前文件
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File dir = new File(mContext.getFilesDir() + File.separator + CRASH + File.separator);
            if (dir.exists()) {
                deleteDir(dir);
            }
            if (!dir.exists()) {
                dir.mkdir();
            }
            fileName = dir.toString() + File.separator + getAssignTime("yyyy_MM_dd_HH_mm_ss") + ".txt";
            try {
                FileOutputStream fos = new FileOutputStream(fileName);
                fos.write(fileName.toString().getBytes());
                fos.flush();
                fos.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }

        }
        return fileName;
    }

    /**
     * 获取存入上传异常的上送 手机的信息
     *
     * @param context
     * @return
     */
    private HashMap obtainSimpleInfo(Context context) {
        HashMap map = new HashMap();
        PackageManager mPackageManager = context.getPackageManager();//获得包管理工具
        PackageInfo mPackageInfo = null;//定义一个包信息
        try {
            // 包信息就可以通过包的管理工具去得到包信息的实例、这样就可以调用包信息里的信息了。
            mPackageInfo = mPackageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        map.put("versionName", mPackageInfo.versionName);
        map.put("versionCode", mPackageInfo.versionCode + "");
        map.put("MODEL", Build.MODEL);
        map.put("SDK_INT", Build.VERSION.SDK_INT + "");
        map.put("PRODUCT", Build.PRODUCT);
        //需要获取手机信息
        map.put("MOBLE_INFO", getMobleInfo());
        return map;
    }

    /**
     * 获取手机信息
     * 通过反射去拿到Build 类中的数据了
     *
     * @return 返回String 手机信息
     */
    private String getMobleInfo() {
        StringBuffer info = new StringBuffer();
        try {
            //field意识是字段、
            Field[] fields = Build.class.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                String value = field.get(null).toString();
                info.append(name + "*" + value + "\n");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return info.toString();
    }

    //崩溃的详细信息。
    private String obtainExceptioninfo(Throwable e) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        e.printStackTrace(printWriter);
        printWriter.close();
        return stringWriter.toString();
    }

    private boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            File[] children = dir.listFiles();
            for (File child : children) {
                child.delete();
            }
        }
        return true;
    }

    private String getAssignTime(String dateFormatStr) {
        DateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
        Long currentTime = System.currentTimeMillis();
        return dateFormat.format(currentTime);
    }

}

  • BaseActivity父类进行捕获
 //自定义异常类
        File file = MyException.getInstance().getCrashFile();
        if (file.exists()) {
            try {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
                int len = 0;
                char[] chars = new char[1024];
                while ((len = isr.read(chars)) != -1) {
                    String message = new String(chars, 0, len);
                    Log.e("mrko",message);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

  • 梳理思路
  1. 集成全局的Thread.UncaughtExceptionHandler类,实现uncaughtException构造方法。并提供给外界获取保存异常文件的方法。

  2. uncaughtException类中进行四步的操作: a. 手机信息。b. 应用信息包名版本。 c. 崩溃的详细信息。 d. 保存当前文件

  3. a.保存手机信息是通过反射build类来进行获取变量和值的。

  4. b.通过packageInfo和packageManger来获取的包中的信息

  5. c.将默认的异常信息也添加到StringBuffer中

  6. d.保存获取到的信息、


参考链接

你可能感兴趣的:(自定义全局异常类)