AndroidUtil - APP崩潰回調處理CrashHandler

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class CrashHandle implements Thread.UncaughtExceptionHandler {
    private static final String TAG = "CrashHandle";

    private static final String DATE_FORMAT = "yyyy-MM-dd-HH-mm-ss";

    private Thread.UncaughtExceptionHandler mDefaultHandler;

    private static CrashHandle sMe = null;

    private CrashHandle(Context context) {
        init(context);
    }

    public static CrashHandle getInstance(Context context) {
        if (null == sMe) {
            LogUtil.d(true, TAG, "getInstance() sMe=null");

            sMe = new CrashHandle(context);
            if (null == sMe) {
                throw new IllegalStateException("No CrashHandle here!");
            }
        }
        return sMe;
    }

    public final void init(Context context) {
        LogUtil.d(true, TAG, "init()");

        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);

        LogUtil.d(true, TAG, "defautHandle = " + Thread.getDefaultUncaughtExceptionHandler());
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        LogUtil.e(true, TAG, "uncaughtException() thread = " + thread + ", ex = " + ex);

        handleException(ex);
        if (null != mDefaultHandler) {
            mDefaultHandler.uncaughtException(thread, ex);
        }
    }

    private boolean handleException(Throwable ex) {
        if (null == ex) {
            return false;
        }

        LogUtil.e(true,TAG, ex.toString());
        saveCrashInfo(ex);

//        ThreadPoolUtils.execute(new Runnable() {
//            @Override
//            public void run() {
//                Looper.prepare();
//
//                ToastUtil.showShortToast(mContext, "Exception!!!!!!!");
//                Looper.loop();
//            }
//        });

        return true;
    }

    /**
     * 保存錯誤信息到文件中
     */
    private void saveCrashInfo(Throwable ex) {

        LogUtil.d(true,TAG, "saveCrashInfo()");

        FileOutputStream fos = null;
        PrintStream printStream = null;
        try {
            SimpleDateFormat mFormatter = new SimpleDateFormat(DATE_FORMAT, Locale.CHINA);
            String time = mFormatter.format(new Date());
            String fileName = LogUtil.APP_NAME + "-crash-" + time + ".0";

            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/smarthome/";
            File dir = new File(path);
            if (!dir.exists()) {
                if (!dir.mkdirs()) {
                    LogUtil.e(true,TAG, "dir.mkdirs fail");
                }
            }
            fos = new FileOutputStream(path + fileName, true);
            printStream = new PrintStream(fos, true, "UTF-8");
            if(null != ex){
                ex.printStackTrace(printStream);
            }
            printStream.flush();
        } catch (FileNotFoundException e) {
            LogUtil.e(true, TAG, e.getMessage());
        } catch (UnsupportedEncodingException e) {
            LogUtil.e(true, TAG, e.getMessage());
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    LogUtil.e(true,TAG, "close fos fail", e);
                }
            }
            if (null != printStream) {
                printStream.close();
            }
        }
    }
}
更穩妥的情況下,另起進程執行saveCrashInfo
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章