The source code is as follows:
PackageCom.demo.app;ImportJava.io.File;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.PrintWriter;ImportJava.lang.Thread.UncaughtExceptionHandler;Importjava.net.ConnectException;Importjava.net.SocketException;Importjava.net.UnknownHostException;Importjava.util.Date;Importorg.apache.commons.httpclient.HttpException;ImportCom.demo.app.common.UIHelper;ImportAndroid.content.Context;ImportAndroid.content.pm.PackageInfo;Importandroid.os.Environment;ImportAndroid.os.Looper;ImportAndroid.widget.Toast;/*** Application Exception class: Used to catch exceptions and prompt for error messages *@version1.0 * @created 2012-3-21*/ Public classAppexceptionextendsExceptionImplementsuncaughtexceptionhandler{Private Final Static BooleanDebug =false;//whether to save the error log /**Defining Exception Types*/ Public Final Static byteType_network = 0x01; Public Final Static byteType_socket = 0x02; Public Final Static byteType_http_code = 0x03; Public Final Static byteType_http_error= 0x04; Public Final Static byteType_xml = 0x05; Public Final Static byteType_io = 0x06; Public Final Static byteType_run = 0x07; Private bytetype; Private intCode; /**system default Uncaughtexception processing class*/ PrivateThread.uncaughtexceptionhandler Mdefaulthandler; Privateappexception () { This. Mdefaulthandler =Thread.getdefaultuncaughtexceptionhandler (); } PrivateAppexception (byteTypeintcode, Exception EXCP) { Super(EXCP); This. Type =type; This. Code =Code; if(Debug) { This. Saveerrorlog (EXCP); } } Public intGetCode () {return This. Code; } Public intGetType () {return This. Type; } /*** Save Exception Log *@paramEXCP*/ Public voidSaveerrorlog (Exception excp) {String errorlog= "Errorlog.txt"; String Savepath= ""; String LogFilePath= ""; FileWriter FW=NULL; PrintWriter PW=NULL; Try { //determine if the SD card is mountedString storagestate =environment.getexternalstoragestate (); if(Storagestate.equals (environment.media_mounted)) {Savepath= Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/oschina/log/"; File File=NewFile (Savepath); if(!file.exists ()) {File.mkdirs (); } LogFilePath= Savepath +errorlog; } //Unable to write file without mounting SD card if(LogFilePath = = ""){ return; } File logFile=NewFile (LogFilePath); if(!logfile.exists ()) {Logfile.createnewfile (); } FW=NewFileWriter (LogFile,true); PW=NewPrintWriter (FW); Pw.println ("--------------------"+(NewDate (). toLocaleString ()) + "---------------------"); Excp.printstacktrace (PW); Pw.close (); Fw.close (); } Catch(Exception e) {e.printstacktrace (); }finally{ if(PW! =NULL) {pw.close ();} if(FW! =NULL){Try{Fw.close ();}Catch(IOException e) {}}} } Public StaticAppexception http (intcode) { return NewAppexception (Type_http_code, CODE,NULL); } Public Staticappexception http (Exception e) {return NewAppexception (type_http_error, 0, E); } Public Staticappexception socket (Exception e) {return NewAppexception (type_socket, 0, E); } Public Staticappexception io (Exception e) {if(Einstanceofunknownhostexception | | Einstanceofconnectexception) { return NewAppexception (type_network, 0, E); } Else if(EinstanceofIOException) { return NewAppexception (type_io, 0, E); } returnrun (e); } Public Staticappexception XML (Exception e) {return NewAppexception (type_xml, 0, E); } Public Staticappexception Network (Exception e) {if(Einstanceofunknownhostexception | | Einstanceofconnectexception) { return NewAppexception (type_network, 0, E); } Else if(EinstanceofHttpException) { returnhttp (e); } Else if(Einstanceofsocketexception) { returnsocket (e); } returnhttp (e); } Public Staticappexception Run (Exception e) {return NewAppexception (Type_run, 0, E); } /*** Get app Exception crash processing Object *@paramContext *@return */ Public Staticappexception Getappexceptionhandler () {return Newappexception (); } @Override Public voiduncaughtexception (thread thread, Throwable ex) {if(!handleexception (ex) && Mdefaulthandler! =NULL) {mdefaulthandler.uncaughtexception (thread, ex); } } /*** Custom Exception handling: Collect error Messages & Send error reports *@paramex *@returntrue: The exception information was processed; false otherwise*/ Private BooleanHandleException (Throwable ex) {if(ex = =NULL) { return false; } FinalContext context =Appmanager.getappmanager (). CurrentActivity (); if(Context = =NULL) { return false; } FinalString Crashreport =Getcrashreport (context, ex); //Show exception information & Send Report NewThread () { Public voidrun () {looper.prepare (); Looper.loop (); }}.start (); return true; } /*** Get app Crash Exception report *@paramex *@return */ PrivateString Getcrashreport (context context, throwable ex) {PackageInfo pinfo=( (AppContext) Context.getapplicationcontext ()). Getpackageinfo (); StringBuffer Exceptionstr=NewStringBuffer (); Exceptionstr.append ("Version:" +pinfo.versionname+ "(" +pinfo.versioncode+ ") \ n"); Exceptionstr.append ("Android:" +android.os.build.version.release+ "(" +android.os.build.model+ ") \ n"); Exceptionstr.append ("Exception:" +ex.getmessage () + "\ n"); Stacktraceelement[] Elements=Ex.getstacktrace (); for(inti = 0; i < elements.length; i++) {exceptionstr.append (elements[i].tostring ()+ "\ n"); } returnexceptionstr.tostring (); }}
Source analysis of "Listviewjson" "Com.demo.app" "appexception" and its role in engineering