Many times when we run the program, once an exception occurs, and we do not capture in the program, no processing, will pop up a
dialog box, causing the program to collapse, the user experience of this situation is very poor, and then how to solve the problem, the program any missing exception handling we can
Caught, is the global exception capture, the code is as follows:
1 PackageCom.qdsj.hxcmjj.util;2 3 ImportJava.lang.Thread.UncaughtExceptionHandler;4 5 ImportAndroid.app.ProgressDialog;6 ImportAndroid.content.Context;7 8 /**9 * Exception Handling classTen * @authorJansin One * A */ - Public classCrashhandlerImplementsUncaughtexceptionhandler { - Public Static FinalString TAG = "Crashhandler"; the Private StaticCrashhandler INSTANCE =NewCrashhandler (); - PrivateContext Mcontext; - ProgressDialog Loadingdialog; - + PrivateCrashhandler () { - } + A Public StaticCrashhandler getinstance () { at returnINSTANCE; - } - - Public voidInit (Context ctx) { -Mcontext =CTX; -Thread.setdefaultuncaughtexceptionhandler ( This); in - //initializing exception handling classes to } + - @Override the Public voiduncaughtexception (Thread arg0, Throwable arg1) { * $ //after catching the exception, do the processing here.Panax Notoginseng } - the +}
Uncaughtexceptionhandler Global exception capture class as shown in the code
With this custom class, we also need to initialize the exception-handling class before the program runs, preferably in the application class, to initialize the global exception-capturing class.
With this class, we can solve the problem that our missed exception capture causes the program to crash.
Android Global catch exception handling