There are many reasons for frequent flashback (ANR) in Android projects, and various exceptions that are not caught can cause the project to crash, and in addition to writing the code is to consider the overall, and to capture the exception that may occur, You can also set a mechanism for global exception trapping to ensure that you are negligent (no handling of exceptions that should be caught). The steps are as follows:
1. First create a handler inheritance Java.lang.Thread.UncaughtExceptionHandler:
Exception not caught
@Override
public void uncaughtexception (thread thread, Throwable ex) {
String threadname = Thread.getname ();
ITKTLOG.D (ThreadName);
Here we can discriminate according to the thread name, and we can also write the exception information to the file for later analysis
}
Singleton Reference, here we make a singleton, because we only need a Uncaughtexceptionhandler instance in an application
private static Crashhandler instance;
Private Crashhandler () {}
Synchronization method to avoid exceptions in single-threaded environments
Public synchronized static Crashhandler getinstance () {
if (instance = = null) {
Instance = new Crashhandler ();
}
return instance;
}
Initialize, set the current object to the Uncaughtexceptionhandler processor
public void init (Context ctx) {
Thread.setdefaultuncaughtexceptionhandler (this);
}
2. Initialize in the OnCreate () method of the custom application:
Crashhandler handler = Crashhandler.getinstance ();
Handler.init (Getapplicationcontext ());
This article is from the "Faith Connaught Group Technology Center" blog, please be sure to keep this source http://sinoteam.blog.51cto.com/9115640/1567452
Resolve Android Flash Back