Android CrashHandler writes its own exception capture class


Catalog (?)[-]

  1. call
  2. test

When writing code, we may throw various exceptions. Some of these exceptions are found and solved during our test, but some of them are unknown to us. We need to understand and solve them in time, whether it's the logic problem of the code or some bug s at the bottom of Android itself. When the user crashes when using the app, we need to know what the reason is, and record the reason to the server, so that we can know the specific reason later
##CrashHandler class

It is used to record the crash reason and save it to sd card.

public class CrashHandler implements Thread.UncaughtExceptionHandler {
    private static final String TAG = "CrashHandler";
    private static final boolean DEBUG = true;
    //File path
    private static final String PATH = Environment.getExternalStorageDirectory().getPath() +File.separator+ "crash";
    private static final String FILE_NAME = "crash";
    private static final String FILE_NAME_SUFEIX = ".trace";
    private static Thread.UncaughtExceptionHandler mDefaultCrashHandler;
    private static CrashHandler mCrashHandler = new CrashHandler();
    private Context mContext;

    private CrashHandler() {
    }

    public static CrashHandler getInstance() {
        return mCrashHandler;
    }

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

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        try {
            //Write files to sd card
            writeToSDcard(ex);
            //Upload operation can be performed here after writing
        } catch (IOException e) {
            e.printStackTrace();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        ex.printStackTrace();
        //If the system provides default exception handling, it will be handed over to the system for handling, otherwise it will handle by itself.
        if (mDefaultCrashHandler != null) {
            mDefaultCrashHandler.uncaughtException(thread, ex);
        } else {
            Process.killProcess(Process.myPid());
        }
    }

    //Write exception to file
    private void writeToSDcard(Throwable ex) throws IOException, PackageManager.NameNotFoundException {
        //If there is no SD card, return directly
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return;
        }
        File filedir = new File(PATH);
        if (!filedir.exists()) {
            filedir.mkdirs();
        }
        long currenttime = System.currentTimeMillis();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(currenttime));

        File exfile = new File(PATH +File.separator+FILE_NAME+time + FILE_NAME_SUFEIX);
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(exfile)));
        Log.e("Error log file path",""+exfile.getAbsolutePath());
        pw.println(time);
        PackageManager pm = mContext.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
        //Current version number
        pw.println("App Version:" + pi.versionName + "_" + pi.versionCode);
        //Current system
        pw.println("OS version:" + Build.VERSION.RELEASE + "_" + Build.VERSION.SDK_INT);
        //manufacturer
        pw.println("Vendor:" + Build.MANUFACTURER);
        //Mobile phone model
        pw.println("Model:" + Build.MODEL);
        //CPU architecture
        pw.println("CPU ABI:" + Build.CPU_ABI);

        ex.printStackTrace(pw);
        pw.close();

    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

call

public class BaseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        CrashHandler crashHandler=CrashHandler.getInstance();
        crashHandler.init(this);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

test

 mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                throw  new RuntimeException("Throw an exception ");
            }
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Finally, remember to add file permissions and call BaseApplication
 

Tags: Android Mobile

Posted on Thu, 30 Apr 2020 09:25:38 -0700 by kaveman50