Android development step by step 57: UncaughtExceptionHandler uncaptured exception Processor
When writing a program, most of the time, we will know how to add try and catch code blocks, such
try { mRoot = inflater.inflate(R.layout.fragment_setting, container, false); initView(mRoot); } catch (Exception e) { log.error("SettingFragment", e); } catch (OutOfMemoryError e) { log.error("SettingFragment.OutOfMemoryError", e); }
However, if you forget to add it, the program will crash and the entire app will stop running, just like this:
Obviously, this user experience is very poor. We need to avoid it. In this case, we need to add a default exception handler to capture exceptions, give the user a prompt or jump to the home page, so as not to bring up a program stop error. Okay, now we will add this default exception handler. Generally, In the override Application class onCreate (), that is, once the app is started, we need to add it.
/*****/Package com. figo. study; import java. io. printWriter; import java. io. stringWriter; import java. io. writer; import java. util. arrayList; import com. figo. study. utils. crashHandler; import android. app. activity; import android. app. application; import android. content. intent; import android. util. log; import android. widget. toast;/*** @ author figo **/public class MainApplication extends Application {@ Override public void onCreate () {super. onCreate (); // set Thread Exception Handler initExHandler ();} public void initExHandler () {// set the CrashHandler as the program's default processor CrashHandler catchExcep = new CrashHandler (this ); thread. setDefaultUncaughtExceptionHandler (catchExcep );}}
Exception Handling class CrashHandler. java
Package com. figo. study. utils; import java. lang. thread. uncaughtExceptionHandler; import android. app. alarmManager; import android. app. pendingIntent; import android. content. context; import android. content. intent; import android. OS. logoff; import android. util. log; import android. widget. toast; import com. figo. study. AActivity; import com. figo. study. mainApplication; public class CrashHandler implements UncaughtExcep TionHandler {private Thread. uncaughtExceptionHandler mDefaultHandler; public static final String TAG = "CatchExcep"; MainApplication application; public CrashHandler (MainApplication application) {// obtain the default UncaughtException processor mDefaultHandler = Thread. getDefaultUncaughtExceptionHandler (); this. application = application ;}@ Override public void uncaughtException (Thread thread, Throwable ex) {if (! HandleException (ex) & mDefaultHandler! = Null) {// if the user does not process the mDefaultHandler, the system's default exception processor is used to process the mDefaultHandler. uncaughtException (thread, ex);} else {Intent intent = new Intent (application. getApplicationContext (), MainActivity. class); PendingIntent restartIntent = PendingIntent. getActivity (application. getApplicationContext (), 0, intent, Intent. FLAG_ACTIVITY_NEW_TASK); // exit the current program and jump to the MainActivity page. class AlarmManager mgr = (AlarmManager) application. getSystemService (Context. ALARM_SERVICE); mgr. set (AlarmManager. RTC, System. currentTimeMillis () + 1000, restartIntent); // restart the application in one second. finishActivity () ;}}/*** custom error handling, collecting error information and sending error reports are all completed here. ** @ param ex * @ return true: If the exception information is processed, false is returned. */private boolean handleException (Throwable ex) {if (ex = null) {return false;} // use Toast to display exception information new Thread () {@ Override public void run () {logoff. prepare (); Toast. makeText (application. getApplicationContext (), "Sorry, the program has an exception and is about to exit. ", Toast. LENGTH_SHORT ). show (); logoff. loop ();}}. start (); return true ;}}