1. 程式人生 > >android系統異常資訊捕獲處理

android系統異常資訊捕獲處理

1.在Android中有的未知的Bug可能在測試中沒有及時發現,導致使用者在使用時出現程式奔潰,出現使用者體驗不好,而我們又不清楚的情況。android提供了Thread.UncaughtExceptionHandler介面,可以通過實現這個介面,在全域性中捕獲異常並處理,上傳到伺服器。

2.實現Thread.UncaughtExceptionHandler介面,設定Thread.setDefaultUncaughtExceptionHandler(this)把異常情況交給自定義的類處理。當程式出現崩潰時,會呼叫

uncaughtException(Thread thread, Throwable throwable)這個回撥方法,異常資訊儲存在Throwable 這個引數。可以把異常資訊輸出儲存到檔案中,一般應用都有啟動頁,可以在啟動頁中判斷是否有儲存異常資訊的檔案,上傳到伺服器,成功之後刪除對應檔案。
public class MyExceptionHandler implements Thread.UncaughtExceptionHandler {
    private static MyExceptionHandler instance;

    private Thread.UncaughtExceptionHandler defaultExceptionHandler;
    private Context context;

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

    public void init(Context context){
        this.context = context;
        Thread.setDefaultUncaughtExceptionHandler(this);                          //異常交給自定義類處理
        defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();     //系統預設的異常處理類
    }

    //當程式崩潰時,會呼叫該回調函式,異常資訊則保持在Throwable中
    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {
        if(throwable != null && context != null){
            saveCrashInfo(throwable);
        }else {
            defaultExceptionHandler.uncaughtException(thread,throwable);         //如果都為空則交給系統預設的UncaughtException處理
        }
    }

    //處理異常資訊
    private void saveCrashInfo(Throwable throwable){
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            File dir =new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"carsh");
            if(!dir.exists()){
                dir.mkdirs();
            }

            long currTime = System.currentTimeMillis();
            File file = new File(dir,currTime+".log");
            if(!file.exists()){
                try {
                    file.createNewFile();
                    BufferedWriter writer = new BufferedWriter(new FileWriter(file));   //儲存異常資訊到檔案中,可上傳到伺服器。在開發本人通過Okhttp上傳檔案。
                    String phoneType = "手機型號="+ Build.MANUFACTURER+" "+Build.MODEL;
                    writer.write(phoneType);
                    writer.write("\r\n");
                    writer.write(throwable.toString());
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            android.os.Process.killProcess(android.os.Process.myPid());   //關閉應用
        }
    }

}
3.在自定義的Applicaiton初始化MyExceptionHandler 。
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        initCrashHandler();
    }

    //在Application進行初始化,當程式出現異常時,由自定義的異常處理類進行處理
    private void initCrashHandler(){
        MyExceptionHandler.getInstance().init(this);
    }

}
4.程式碼地址:http://download.csdn.net/detail/huangma11/9637466