Android 手动抓Crash日志

public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static final String       FILE_NAME        = "crash";
    private static final String       FILE_NAME_SUFFIX = ".trace";
    private static       CrashHandler ch               = new CrashHandler();
    private Thread.UncaughtExceptionHandler mDefaulCrashHandler;
    private Context                         mContext;
    private SimpleDateFormat                mSdf;

    private CrashHandler() {
    }

    public static CrashHandler getInstance() {
        return ch;
    }

    public void init(Context context) {
        mDefaulCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
        mContext = context.getApplicationContext();
        mSdf = (SimpleDateFormat) SimpleDateFormat.getInstance();
        mSdf.applyPattern("yyyy-MM-dd HH:mm:ss");
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        try {
            dumpException(e);
            uploadExceptionToServer();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        e.printStackTrace();
        if (mDefaulCrashHandler != null)
            mDefaulCrashHandler.uncaughtException(t, e);
        else
            Process.killProcess(Process.myPid());
    }

    private void dumpException(Throwable ex) throws IOException {
        String path = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable())
            path = mContext.getExternalFilesDir(null).getAbsolutePath() + "/crash/log";
        else
            path = mContext.getFilesDir().getAbsolutePath() + "/crash/log";
        File dir = new File(path);
        if (!dir.exists())
            dir.mkdirs();
        long current = System.currentTimeMillis();
        String time = mSdf.format(current);
        File file = new File(path + File.separator + FILE_NAME + time + FILE_NAME_SUFFIX);
        try {
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            pw.println(time);
            dumpPhoneInfo(pw);
            pw.println();
            ex.printStackTrace(pw);
            pw.close();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void dumpPhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException {
        PackageManager pm = mContext.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
        pw.print("App Version: ");
        pw.print(pi.versionName);
        pw.print('_');
        pw.println(pi.versionCode);

        //Android版本号
        pw.print("OS Version: ");
        pw.print(Build.VERSION.RELEASE);
        pw.print('_');
        pw.println(Build.VERSION.SDK_INT);

        //手机制造商
        pw.print("Vendor: ");
        pw.println(Build.MANUFACTURER);

        //手机型号
        pw.print("Model: ");
        pw.println(Build.MODEL);

        //CPU架构
        pw.print("CPU ABI: ");
        pw.println(Build.CPU_ABI);
    }

    private void uploadExceptionToServer() {
    }
}

当你的程序崩溃时,会在你的/data/data/你的程序目录/files/crash/log目录中生成崩溃日志,记得加读写权限哦.

1.我们先在Application里面初始化一下
Android 手动抓Crash日志_第1张图片

2.然后随便触发一个bug
Android 手动抓Crash日志_第2张图片
这里大家可以看到,点击按钮后就程序会崩溃,会报一个数组越界异常.

3.我们来验证一下,看在/data/data/你的程序目录/files/crash/log中是否会生成崩溃日.
这里写图片描述
日志是生成了,我们在看看里面到底是不是数组越界异常.
Android 手动抓Crash日志_第3张图片
看到了吧,确实也是数组越界异常,说明捕获崩溃成功.

你可能感兴趣的:(Android 手动抓Crash日志)