bugly打印異常

這是我參考別人的案例 然後寫的一遍博客

導入依賴

implementation 'com.tencent.bugly:crashreport:2.8.6'

創建一個類 繼承application如果有的話直接往裏面加入


public class MyApplication extends Application {
 
    @Override
    public void onCreate() {
        super.onCreate();
        UnCatchHandler.getInstance(getApplicationContext()).init(getApplicationContext());
        //下面爲把異常保存到SD的代碼
        //CrashReport.initCrashReport(getApplicationContext(), "f9f67393ee", true);
    }
}

全局捕獲異常

/**
 * 全局捕獲異常類,實現Thread.UncaughtExceptionHandler
 * @author hasee
 */
public class UnCatchHandler implements Thread.UncaughtExceptionHandler {
    private static UnCatchHandler mUnCatchHandler;
    private Context mContext;
 
    /**
     * 一定要寫個單例
     * @return
     */
    public static UnCatchHandler getInstance(Context context) {
        if(mUnCatchHandler == null){
            synchronized (UnCatchHandler.class) {
                mUnCatchHandler = new UnCatchHandler(context);
            }
        }
        return mUnCatchHandler;
    }
 
    private UnCatchHandler(Context context) {
        init(context);
    }
 
    public void init(Context context) {
        //獲取默認的系統異常捕獲器
        //把當前的crash捕獲器設置成默認的crash捕獲器
        Thread.setDefaultUncaughtExceptionHandler(this);
        mContext = context.getApplicationContext();
    }
 
    /**
     * @param t
     * @param e
     */
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        try {
            //打印異常信息
            Log.i("1607C", e.getLocalizedMessage());
            //此處調用保存到SD的方法傳參
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

清單文件中 寫入name屬性

//下面爲把捕獲到的異常保存到SD中的代碼

/**
     * 存儲sd卡
     */
    private void saveSD(Throwable throwable) throws Exception {
        //判斷SD卡狀態
        //MEDIA_MOUNTED 存儲媒體已經掛載,並且掛載點可讀/寫
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //徹底退出
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
            return;
        }
 
        //獲取手機的一些信息
        PackageManager pm = mContext.getPackageManager();
        PackageInfo inFo = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
 
        //獲取版本信息
        String versionName = inFo.versionName;
        int versionCode = inFo.versionCode;
 
        int version_code = Build.VERSION.SDK_INT;
 
        //Android版本號
        String release = Build.VERSION.RELEASE;
        //手機型號
        String mobile = Build.MODEL;
 
        //手機製造商
        String mobileName = Build.MANUFACTURER;
 
        //存儲至sdCard下
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
 
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String time = simpleDateFormat.format(new Date());
 
        //找到文件夾路徑,創建exception文件夾
        File f = new File(path, "exception");
        f.mkdirs();
 
        //找到文件夾路徑,查找exception + time的txt文件夾
        File file = new File(f.getAbsolutePath(), "exception" + time + ".txt");
 
        //如果文件沒有,則創建
        if (!file.exists()) {
            file.createNewFile();
        }
 
        String data = "\nMobile型號:" + mobile + "\nMobileName:" + mobileName + "\nSDK版本:" + version_code +
                "\n版本名稱:" + versionName + "\n版本號:" + versionCode + "\n異常信息:" + throwable.toString();
 
        Log.i("dj", data);
 
        //以下是寫入文件
        byte[] buffer = data.trim().getBytes();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        // 開始寫入數據到這個文件。
        fileOutputStream.write(buffer, 0, buffer.length);
        fileOutputStream.flush();
        fileOutputStream.close();
 
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章