1. Description of the problem
Flash-Back (Crash) is the performance of the client program when it encounters an unhandled exception or error at run time, and gives its own solution from the Crash of the reason for the occurrence of the problem and the solution, and how to capture and analyze the exception after the Crash has occurred.
We use the Android platform as an example of how to capture the flashback information for Android apps to help us locate and resolve the problem code that caused the flash back.
2. Flash back in Android
Before we talk about the flashback in Android, let's start by simply reviewing the exceptions in Java.
(1) Exceptions in Java
The exception hierarchy in Java is as follows:
We can see that the Throwable class is the base class in the exception hierarchy. The error class represents an internal error that is beyond our control; exception represents an exception, RuntimeException and its subclasses belong to an unchecked exception, such exceptions include ArrayIndexOutOfBoundsException, NullPointerException, we should avoid the occurrence of unchecked anomalies by means of conditional judgment and other statements. IOException and its subclasses are checked exceptions, and the compiler checks to see if we provide an exception handler for all the checked exceptions that might be thrown, and if not, an error. For unchecked exceptions, we don't need to capture (and of course Java also allows us to capture, but what we should do to avoid unchecked exceptions).
Knowing this, we can probably know that no matter what the Android app is for, we can just catch the exception that caused the flashback, and we'll be able to handle the exception. For checked exceptions It is good to say that the number is relatively small and the compiler's coercion ensures that we set the exception handler for the checked exceptions that may be thrown in all the code. However, for unchecked exceptions, it is difficult to predict what exceptions the code will trigger in some extreme situations (such as inferior SD cards, bad speeds), making it difficult to catch all possible unchecked exceptions. The good news is that Android gives us a mechanism to get the flashback information for Android apps, so let's take a look.
(2) Catch the exception that caused the flash back
An Uncaughtexceptionhandler object exists in the running Android process (no exception handler is caught), We can set the Uncaughtexceptionhandler object of the current process through the static method of the thread class Setdefaultuncaughtexceptionhandler. When an uncaught exception occurs, the system invokes the Uncaughtexception method of the Uncaughtexceptionhandler object of the process in which the exception occurred, that is, we can get the exception information in this method that causes the flashback before the Android app flashes back. Uncaughtexceptionhandler is an interface in which only one uncaughtexception method is defined.
Based on the above analysis we know: As long as the current process of the uncaught exception handler is set to our custom Uncaughtexceptionhandler instance, We can override the Uncaughtexceptionhandler method to get the details of the exception that caused the flashback before the flash-back. Our custom uncaught exception handler implements the Uncaughtexceptionhandler interface in the following code:
Public class Implements Uncaughtexceptionhandler { @Override publicvoid Uncaughtexception (Thread T, Throwable e) { try { // The parameter e that passed in this method is the exception that causes the application crash, we can get the exception information here, can upload the exception information to the server for unified analysis, can also be saved in the file system catch ( Exception e) { ... } }}
After designing the custom uncaught exception handler, we simply set it as an uncaught exception handler for the current process, with the following code:
Thread.setdefaultuncaughtexceptionhandler (new Crashhandler ());
(3) Third-party solutions
Crash analysis and processing of Android applications, there are many mature third-party solutions, such as bugly, ACRA, etc., if you are interested in the self-understanding.
A brief analysis of the test questions of Ali client engineers--android application of Flash back (crash) analysis