Android Crash 监控方案

·  阅读 164

Crash 监控方案
1 java
1 UncaughtExceptionHandler

package com.enjoy.crash2;
import android.content.Context;
import java.io.File;

public class CrashReport {

    public static void init(Context context) {
        Context applicationContext = context.getApplicationContext();
        CrashHandler.init(applicationContext);
    }

    public static void testJavaCrash() {
        int i = 1 / 0;
    }
}
复制代码
package com.enjoy.crash2;

import android.content.Context;

import androidx.annotation.NonNull;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler;
    private static Context mContext;

    private CrashHandler() {

    }

    public static void init(Context applicationContext) {
        mContext = applicationContext;
        defaultUncaughtExceptionHandler =
                Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(new CrashHandler());
    }

    @Override
    public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
        File dir = new File(mContext.getExternalCacheDir(), "crash_info");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        long l = System.currentTimeMillis();
        File file = new File(dir, l + ".txt");

        try {
            PrintWriter pw = new PrintWriter(new FileWriter(file));
            pw.println("time: "+time);
            pw.println("thread: " + t.getName());
            pw.println(getPhoneInfo());
            e.printStackTrace(pw);
            pw.flush();
            pw.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } finally {
            if (defaultUncaughtExceptionHandler != null) {
                defaultUncaughtExceptionHandler.uncaughtException(t, e);
            }
        }
    }
    private String getPhoneInfo() throws PackageManager.NameNotFoundException{ 
    			PackageManager pm = mContext.getPackageManager(); 
    			PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES); 
    			StringBuilder sb = new StringBuilder(); 
    			//App版本 
    			sb.append("App Version: "); 
    			sb.append(pi.versionName); 
    			sb.append("_"); 
    			sb.append(pi.versionCode + "\n"); 
    			//Android版本号 
    			sb.append("OS Version: "); 
    			sb.append(Build.VERSION.RELEASE); 
    			sb.append("_"); 
    			sb.append(Build.VERSION.SDK_INT + "\n"); 
    			//手机制造商 
    			sb.append("Vendor: "); 
    			sb.append(Build.MANUFACTURER + "\n"); 
    			//手机型号 
    			sb.append("Model: "); 
    			sb.append(Build.MODEL + "\n");
	}

Applciation中 初始化
CrashReport.init(this);
复制代码

2 native
1 BreakPad 处理
3 墓碑机制
程序奔溃后linux产生一个记录奔溃的现场信息的文件
路径 data/tombstones

分类:
Android
标签:
分类:
Android
标签:
收藏成功!
已添加到「」, 点击更改