public class Crashhandler implements uncaughtexceptionhandler{private static Crashhandler instance; public static Final string TAG = "Crashhandler"; private static final String version_name = "Versionname"; private static FINA L String version_code = "Versioncode"; private static final String stack_trace = "Stack_trace"; private Thread. Uncaughtexceptionhandler mdefaulthandler; private Context mcontext; private static final String CRASH _reporter_extension = ". Cr";p rivate Properties Devicecrashinfo = new properties (); private Crashhandler () {} public static synchronized Crashhandler getinstance () {if (instance = = null) {instance = new Crashhandler ();} return instance;} public void init (Context ctx) {this.mcontext = Ctx;mdefaulthandler = Thread.getdefaultuncaughtexceptionhandler () ; Thread.setdefaultuncaughtexceptionhandler (this);} @Overridepublic void uncaughtexception (thread thread, Throwable ex) {if (Mdefaulthandler! = null &&!handlerexception (ex)) {//If the user does not perform exception handling let the system default processor handle Mdefaulthandler.uncaughtexception (thread, ex);} Else{exitapp () ;}} private boolean handlerexception (Throwable ex) {if (ex = = null) {return true;} Final String msg = Ex.getlocalizedmessage (); new Thread () {@Overridepublic void run () {looper.prepare (); Toast toast = Toast.maketext (Mcontext, "The program went wrong!") "+ msg, toast.length_short); toast.setgravity (gravity.center, 0, 0); Toast.show (); Looper.loop ();}}. Start ();//Collect device Information collectdeviceinfo (mcontext);//Save Error Savecrashinfotofiles (ex);// Send error report to server Sendcrashreportstoserver (Mcontext); return true;} private void Collectdeviceinfo (Context ctx) {Try{packagemanager pm = Ctx.getpackagemanager (); PackageInfo pi = pm.getpackagearchiveinfo (Ctx.getpackagename (), packagemanager.get_activities), if (pi! = null) { Devicecrashinfo.put (version_name, pi.versionname = = null?) "Not set": Pi.versionname);d evicecrashinfo.put (Version_code, Pi.versioncode);}} catch (Exception e) {log.e (TAG, "error occured when collecting crash infO "+ e);} &NBSP;//uses reflection to collect device information. The build class contains various device information,//For example: System version number, device manufacturer and other useful information to help debug the program//For specific information, refer to the following field[] fields = Builder.class.getDeclaredFields (); for (Field field:fields) {try{field.setaccessible (true);d Evicecrashinfo.put ( Field.getname (), null);} catch (Exception e) {log.e (TAG, "error occured when collecting device info" + e);}}} private String Savecrashinfotofiles (Throwable ex) {Writer info = new StringWriter (); PrintWriter printwriter = new PrintWriter (info); Ex.printstacktrace (printwriter); throwable cause = Ex.getCause () ; while (cause! = null) {ex.printstacktrace (printwriter); cause = Cause.getcause ();} string result = info.tostring ();p rintwriter.close ();d evicecrashinfo.put ("EXCEPTION", Ex.getlocalizedmessage ( ));d Evicecrashinfo.put (stack_trace, result); try{long time = System.currenttimemillis (); String filename = "crash-" + Time + crash_reporter_extension; FileOutputStream trace = mcontext.openfileoutput (filename, context.mode_private); devicecrashinfo.store (Trace , ""); Trace.flush (); Trace.close ();} catch (Exception e) {log.e (TAG, "error occured when save Crashinfo to files" + E);} return null;} private void Sendcrashreportstoserver (Context ctx) {string[] crfiles = Getcrashreportfiles (CTX); if (crfiles! = Null && crfiles.length > 0) {treeset<string> sortedfiles = new treeset<string> (); Sortedfiles.addall (Arrays.aslist (Crfiles)); for (String filename:sortedfiles) {file CR = new File (Ctx.getfilesdir (), FileName);p Ostreport (CR); Cr.delete ()//delete sent report}}} private void Postreport (file file) {//TODO send error report to server} private string[] Getcrashreportfiles (Context ctx) {File filesdir = Ctx.getfilesdir (); FilenameFilter filter = new FilenameFilter () {public Boolean accept (File dir, String name) {return Name.endswith (crash_rep orter_extension);}}; return filesdir.list (filter);} private void Exitapp () {android.os.Process.killProcess (Android.os.Process.myPid ()); System.exit (0);}} Travel Network, a free dating site: www.jieberu.com push-push family, free tickets, scenic spots: Www.tuituIzu.com
Android handles uncaught exceptions (applying global exceptions)