通過自定義handler實現Thread.UncaughtExceptionHandler,實現全域性捕獲異常,或者第二種方式可以通過騰訊bugly自動整合,可在bugly文件中心檢視使用指南

ren18234073466發表於2018-12-06

全域性捕獲異常類需要的的許可權

//向sd卡寫的許可權
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
//讀sd卡的許可權
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    //獲取手機資訊許可權
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    //獲取網路請求的許可權
    <uses-permission android:name="android.permission.INTERNET" />
    //獲取網路狀態許可權
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    //獲取WiFi狀態許可權
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    //讀取log日誌許可權
    <uses-permission android:name="android.permission.READ_LOGS"
        tools:ignore="ProtectedPermissions" />

UnCatchHandler異常捕獲

package com.bawei.uncatchhandler;

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

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 全域性捕獲異常類,實現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();
    }

    /**
     * 儲存我們丟擲的異常至SD卡
     * @param t
     * @param e
     */
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        try {
            //如果不需要寫到SD卡,則不需要開啟以下注釋
//            saveSD(e);

            //列印異常資訊
            Log.i("1607C", e.getLocalizedMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

//    /**
//     * 儲存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);
//    }
}

MyApplication 需要清單檔案中註冊

package com.bawei.uncatchhandler;

import android.app.Application;

import com.tencent.bugly.crashreport.CrashReport;

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        UnCatchHandler.getInstance(getApplicationContext()).init(getApplicationContext());
//        CrashReport.initCrashReport(getApplicationContext(), "f9f67393ee", true);
    }
}

自動整合bugly文件網址
https://bugly.qq.com/docs/user-guide/instruction-manual-android/?v=20181014122344

相關文章