Reprint Address: http://zheyiw.iteye.com/blog/1670990
Personal Note: general application1, collect all avtivity to completely exit the application2, catch the crash exception, save the error log, and restart the app Public classHkbaseapplicationextendsApplication {//Activity object list for unified management of activity PrivateList<activity>activitylist; //Exception Capture protected BooleanIsneedcaughtexeption =true;//whether to catch unknown exceptions Privatependingintent restartintent; PrivateMyuncaughtexceptionhandler Uncaughtexceptionhandler; PrivateString Packgename; @Override Public voidonCreate () {Super. OnCreate (); Activitylist=NewArraylist<activity>(); Packgename=Getpackagename (); if(isneedcaughtexeption) {cauchexception (); } } //Restart the system after-------------------exception capture-----catch Exception-----------------// Private voidcauchexception () {Intent Intent=NewIntent (); //parameter 1: Package name, Parameter 2: Activity at the program entryIntent.setclassname (Packgename, Packgename + ". Loginactivity "); Restartintent= Pendingintent.getactivity (Getapplicationcontext (), 1, intent, intent.flag_activity_new_task); //trigger thread When program crashesUncaughtexceptionhandler =NewMyuncaughtexceptionhandler (); Thread.setdefaultuncaughtexceptionhandler (Uncaughtexceptionhandler); } //Create a service to catch a crash exception Private classMyuncaughtexceptionhandlerImplementsUncaughtexceptionhandler {@Override Public voiduncaughtexception (thread thread, Throwable ex) {//Save error logSavecatchinfo2file (ex); //Restart the app after 1 secondsAlarmmanager mgr =(Alarmmanager) Getsystemservice (Context.alarm_service); Mgr.set (ALARMMANAGER.RTC, System.currenttimemillis ()+ 1000, restartintent); //Close the current appfinishallactivity (); Finishprogram (); } }; /*** Save the error message to the file * *@returnreturn file name*/ PrivateString Savecatchinfo2file (Throwable ex) {writer writer=NewStringWriter (); PrintWriter PrintWriter=NewPrintWriter (writer); Ex.printstacktrace (PrintWriter); Throwable cause=Ex.getcause (); while(Cause! =NULL) {cause.printstacktrace (printwriter); Cause=Cause.getcause (); } printwriter.close (); String SB=writer.tostring (); Try{DateFormat Formatter=NewSimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss"); String Time= Formatter.format (NewDate ()); String FileName= time + ". txt"; System.out.println ("FileName:" +fileName); if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {String FilePath= Environment.getexternalstoragedirectory () + "/hkdownload/" +Packgename+ "/crash/"; File dir=NewFile (FilePath); if(!dir.exists ()) { if(!Dir.mkdirs ()) { //failed to create directory: generally because the SD card is unplugged return""; }} System.out.println ("FilePath + fileName:" + FilePath +fileName); FileOutputStream Fos=NewFileOutputStream (FilePath +fileName); Fos.write (Sb.getbytes ()); Fos.close (); //after the file is saved, check the error log when the application is started, and find the new error log, send it to the developer } returnFileName; } Catch(Exception e) {System.out.println ("An error occured while writing file ..." +e.getmessage ()); } return NULL; } //------------------------------Activity Management-----------------------// //Activity Management: Remove activity from the list Public voidremoveactivity (activity activity) {activitylist.remove (activity); } //activity Management: Adding activity to the list Public voidaddactivity (activity activity) {Activitylist.add (activity); } //activity Management: End all activity Public voidfinishallactivity () { for(Activity activity:activitylist) {if(NULL!=activity) {activity.finish (); } } } //end thread, typically used with finishallactivity ()//For example: Finishallactivity;finishprogram (); Public voidFinishprogram () {android.os.Process.killProcess (Android.os.Process.myPid ()); }}
"Reprint" General application completely quit the app to get a crash exception, save the error log, and restart the app