2016 February 01

Android App CrashLog 捕获

认可Bug存在的必然性,故而需要捕获FC 问题Crash Log便于分析改进;

public class CrashHandle implements Thread.UncaughtExceptionHandler {

    private static final String CRASH_LOG_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + "lee" + File.separator;

    private static final String CRASH_FILE = "crash_log.trace";

    private static CrashHandle sHandleInstance;

    private Context mAppContext;

    /**
     * 系统默认异常捕获管理
     */
    private Thread.UncaughtExceptionHandler mAppDefaultCrashHandler;

    public static CrashHandle getInstance(Context context) {
        if (sHandleInstance == null) {
            sHandleInstance = new CrashHandle();
            sHandleInstance.mAppContext = context.getApplicationContext();
            sHandleInstance.mAppDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
            Thread.setDefaultUncaughtExceptionHandler(sHandleInstance);
        }
        return sHandleInstance;
    }


    /**
     * 线程被未捕获的异常终止后的回调函数
     * @param thread
     * @param ex
     */
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        try {
            exportExceptions(ex);
        } catch (IOException e) {
            e.printStackTrace();
        }
        uploadCrashFileToServer();
        if (sHandleInstance.mAppDefaultCrashHandler != null) {
            //Use default Handler
            sHandleInstance.mAppDefaultCrashHandler.uncaughtException(thread, ex);
        } else {
           // Clear FC dialog
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }


    /**
     * 输出 Exception 到指定文件
     * @param ex
     */
    private void exportExceptions(Throwable ex) throws IOException {
        File crashFile = new File(CRASH_LOG_PATH + CRASH_FILE);
        if (crashFile.exists()){
            appendExceptionToFile(crashFile, ex);
        }else {
            if (crashFile.createNewFile()){
                appendExceptionToFile(crashFile,ex);
            }else {
                throw new RuntimeException("Create Crash Log Fail()");
            }
        }
    }

    /**
     *将异常信息Append到文件末尾中
     * @param crashFile
     * @param ex
     */
    private void appendExceptionToFile(File crashFile,Throwable ex) {


    }


    /**
     *上传CrashLog到服务器
     * CRASH_FILE:crash_log.trace
     * CRASH_LOG_PATH :sdcard/lee/
     */
    private void uploadCrashFileToServer() {



    }
}

todo


Quote:

Android程序Crash时的异常上报

收集app崩溃crash log

上一篇
下一篇
Loading Disqus comments...
Table of Contents