About crash capture

Keywords: Android network

About crash capture

   Android applications will inevitably crash, also known as crash, which may be due to the underlying bug s of Android system, or due to inadequate model adaptation or poor network conditions. When crash occurs, the system will kill the executing program, which means flashing back or prompting the user that the program has stopped executing.

   Android provides a solution to obtain crash information of the application. There is a method setDefaultUncaughtExceptionHandler in Thread, as shown below.

public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
    // Android-removed: SecurityManager stubbed out on Android
    /*
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(
            new RuntimePermission("setDefaultUncaughtExceptionHandler")
                );
    }
    */

     defaultUncaughtExceptionHandler = eh;
 }

                         . This means that we can get crash exception information in uncaughtException method. We can choose to store the exception information in SD card, and then upload the crash information to the server through the network at the right time (so we can do whatever we want, hhhh hh).

The implementation steps of a typical exception handler are:
                           .
   ② call the setdefaultuncautexceptionhandler method of Thread to set it as the Thread's default exception handler. Because the default exception handler is a static member of the Thread class, it acts on all threads of the current process.
A typical exception handler is implemented as follows (the author briefly deals with the exception information code).

public class CrashHandler implements Thread.UncaughtExceptionHandler {
    
    private static CrashHandler sInstance;
    private Thread.UncaughtExceptionHandler mDefaultCrashHandler;
    private Context mContext;
    
    private CrashHandler() {
    }
    
    public static CrashHandler getInstance() {
        if (sInstance == null) {
            synchronized (CrashHandler.class) {
                if (sInstance == null) {
                    sInstance = new CrashHandler();
                }
            }
        }
        return sInstance;
    }
    
    public void init(Context context) {
        mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
        mContext = context.getApplicationContext();
    }


    /**
     * Get exception information and handle it
     * @param t
     * @param e
     */
    @Override
    public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
        // ...
    }
}

   with CrashHandler, you only need to set the CrashHandler for the thread during Application initialization, as shown below.

public class BaseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this);
    }
}

                    . It should be noted that the caught exceptions in the code will not be handed over to CrashHandler for processing, and CrashHandler can only receive those exceptions that are not caught.

Reference: Android development art exploration

Posted by Drayton on Wed, 17 Jun 2020 01:20:03 -0700