In the Android development will inevitably appear program crash, commonly known as crash. Random access to the user when the test unknown bugs caused by our program crash, at this time we can not get directly to the error log, can not fix the bug. This will greatly affect the user experience, at this time we need to register a function to capture the global exception information, when the program appears crash information, we log the error log, upload to the server, so that we can fix bugs in time. To implement this function we need to rely on Uncaughtexceptionhandler this class, Uncaughtexceptionhandler is an interface, in thread. There is only one way to uncaughtexception. When we sign up for a uncaughtexceptionhandler, when our program crash, the Uncaughtexception method is recalled, and the Uncaughtexception method takes two parameters, The crash information is stored in the parameter. Next look at the write code
Package Hi.xiaoyu.crashhandler;
Import Java.io.File;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.lang.Thread.UncaughtExceptionHandler;
Import Java.util.Date;
Import Android.content.Context;
Import android.os.Environment;
Import Android.util.Log;
public class Crashhandler implements Uncaughtexceptionhandler {private static Crashhandler instance;
public static Crashhandler getinstance () {if (instance = null) {instance = new Crashhandler ();
return instance;
} public void init (context ctx) {Thread.setdefaultuncaughtexceptionhandler (this); /** * Core method, when the program crash callback this method, Throwable in this error log/@Override public void Uncaughtexception (Thread arg0, Throw
Able arg1) {String logPath; if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {LogPath = Environment.getext
Ernalstoragedirectory (). GetAbsolutePath () + File.separator + file.separator + "Log";
File File = new file (LogPath);
if (!file.exists ()) {file.mkdirs ();
try {FileWriter fw = new FileWriter (LogPath + file.separator + "Errorlog.log", true);
Fw.write (New Date () + "\ n");
Error message//Here you can also add the current system version, model models and so on information stacktraceelement[] StackTrace = Arg1.getstacktrace ();
Fw.write (Arg1.getmessage () + "\ n");
for (int i = 0; i < stacktrace.length i++) {fw.write ("file:" + stacktrace[i].getfilename () + Class: "
+ stacktrace[i].getclassname () + "method:" + stacktrace[i].getmethodname () + "line:"
+ stacktrace[i].getlinenumber () + "\ n");
} fw.write ("\ n");
Fw.close ();
Upload error message to server//Uploadtoserver ();
catch (IOException e) {log.e ("Crash handler", "Load file failed ...", e.getcause ());
} arg1.printstacktrace (); Android.os.Process.killProceSS (Android.os.Process.myPid ());
}
}
Register in activity or application
Crashhandler Crashhandler = Crashhandler.getinstance ();
Crashhandler.init (Getapplicationcontext ());
This realizes the Android global exception capture processing, the implementation process is also relatively simple, I hope to learn about Android software programming help.