(Notes) Android gets UncaughtException

    The application crashed recently during development, but I couldn't see the crash information at that time, so I couldn't find the problem quickly. Later, in the book, I found the method to obtain the crash information of the application and recorded it.

    When a crash occurs, the system will kill the program being executed, and a flashback occurs or the user program is prompted to stop running. The developer also cannot directly know why the program crashed. Android provides methods for handling uncaught exceptions. You can monitor the crash information of the application through UncaughtExceptionHandler, and set an UncaughtExceptionHandler for the program. When a crash occurs, the uncaughtException method of UncaughtExceptionHandler will be called . Exception information can be obtained in the uncaughtException method, and you can choose to store the exception information.

import android.content.Context;
import android.os.Environment;
import android.os.Process;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Rewufu on 2016/2/17.
 */
public class CrachHandler implements Thread.UncaughtExceptionHandler {
    private static CrachHandler crachHandler = new CrachHandler();
    private Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
    private Context mContext;
    private static final String PATH = Environment.getExternalStorageDirectory().getPath()+"/UncaughtException/";
    private static final String FILE_NAME = "crash";
    private static final String FILE_NAME_SUFFIX = ".trace";

    public static CrachHandler getInstance() {
        return crachHandler;
    }

    public void init(Context context) {
        uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
        mContext = context;
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {

        try {
            dumpExceptionToSDcard(ex);
        } catch (IOException e) {
            e.printStackTrace ();
        }

        if(uncaughtExceptionHandler != null){
            uncaughtExceptionHandler.uncaughtException(thread, ex);
        }else {
            Process.killProcess(Process.myPid());
        }

    }

    private void dumpExceptionToSDcard(Throwable ex) throws IOException{
        if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            Log.w("CrachHandler", "sdcard unmounted,skip dump exception");
            return;
        }

        File dir = new File(PATH);
        if(!dir.exists()){
            dir.mkdir ();
        }
        long current = System.currentTimeMillis();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(current));
        File file = new File(PATH + FILE_NAME + time + FILE_NAME_SUFFIX);
        PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter(file)));
        printWriter.print(time);
        printWriter.println();
        ex.printStackTrace(printWriter);
        printWriter.close();
    }
}

     The above is the code implementation. First, implement an UncaughtExceptionHandler object, obtain and store the exception information in its uncaughtException method, and call Thread's setDefaultUncaughtExceptionHandler to set it as the thread's default exception handler.

    You can choose to set a CrashHandler for the thread when the Application is initialized.

import android.app.Application;

/**
 * Created by Rewufu on 2016/2/17.
 */
public class Myapplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        CrachHandler crachHandler = CrachHandler.getInstance();
        crachHandler.init(getApplicationContext());
    }
}

     Manually simulate a crash, throw a RuntimeException to test it, and the crash will be saved.

                throw new RuntimeException("Throw RuntimeException yourself");

     

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327038601&siteId=291194637