Let me just say how to collect the exception information of the program running process. Need a friend to come over and refer to the next
When we write a program, we want to write a program that doesn't have any bugs, and expect no program crashes under any circumstances. But the ideal is plump, the reality is bone feeling. No programmer can guarantee that the program you write will never break down abnormally. Especially for the number of users to reach hundreds of thousands of millions of program, when you reach a certain number of users, even if your program appears to have individual abnormal crash situation also do not be surprised.
Now that we've written a program that can crash, and if it's a program that hasn't been released, we can analyze it by testing the crawl log. However, for the published program, we can not reproduce the phenomenon, so let the user feedback program exception information is very important. Let's talk about how to collect exception information for the program's running process.
1. Android Exception Capture interface
Copy Code code as follows:
///When the thread terminates abruptly due to an unhandled exception, the interface to the handler is invoked
Static Interface Uncaughtexceptionhandler
2. Set Thread catch exception
from the above interface we can see that this interface is for the thread said, that is, if we need to monitor the operation of a thread, as long as the interface is implemented, and then set the monitoring method to the specific thread. In general, what we need to monitor most is our UI thread, which is the main line.
Copy Code code as follows:
//Sets the default handler that is invoked when a thread abruptly terminates because it has not been caught by an exception, and no other handlers are defined for that thread.
static void Setdefaultuncaughtexceptionhandler (Thread.uncaughtexceptionhandler eh)
3. Uncaughtexceptionhandler Example
Copy Code code as follows:
Class Mythoucrashhandler implements Uncaughtexceptionhandler
{
private static final String TAG = "Mythoucrashhandler---->";
private Uncaughtexceptionhandler Defaultueh;
//constructors, obtaining the default processing method
public Mythoucrashhandler ()
{
This.defaultueh = Thread.getdefaultuncaughtexceptionhandler ();
}
//This interface must be rewritten to handle our exception information
@Override
public void uncaughtexception (thread thread, throwable ex)
{
final Writer result = new StringWriter ();
final PrintWriter printwriter = new PrintWriter (result);
Get track of stack information, in addition to the system stack information, but also the phone model, system version, compiled version of the unique logo
stacktraceelement[] trace = Ex.getstacktrace ();
stacktraceelement[] Trace2 = new STACKTRACEELEMENT[TRACE.LENGTH+3];
system.arraycopy (Trace, 0, trace2, 0, trace.length);
trace2[trace.length+0] = new Stacktraceelement ("Android", "MODEL", Android.os.Build.MODEL,-1);
trace2[trace.length+1] = new Stacktraceelement ("Android", "VERSION", Android.os.Build.VERSION.RELEASE,-1);
trace2[trace.length+2] = new Stacktraceelement ("Android", "fingerprint", Android.os.Build.FINGERPRINT,-1);
//Append information, because the default processing method is later recalled
Ex.setstacktrace (TRACE2);
Ex.printstacktrace (PrintWriter);
//The stack information obtained above is converted to a string and printed out
String stacktrace = result.tostring ();
Printwriter.close ();
log.e (TAG, StackTrace);
//Here just the exception stack information written to the SD card log inside
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted))
{
String Sdcardpath = Environment.getexternalstoragedirectory (). GetPath ();
Writelog (stacktrace, Sdcardpath + "/mythou");
}
defaultueh.uncaughtexception (thread, ex);
}
//write log information method, write to SD card
private void Writelog (string log, string name)
{
charsequence timestamp = Dateformat.format ("Yyyymmdd_kkmmss", System.currenttimemillis ());
String filename = name + "_" + timestamp + ". Log";
Try
{
FileOutputStream stream = new FileOutputStream (filename);
OutputStreamWriter output = new OutputStreamWriter (stream);
bufferedwriter bw = new BufferedWriter (output);
//write related log to file
bw.write (log);
Bw.newline ();
Bw.close ();
Output.close ();
}
catch (IOException e)
{
E.printstacktrace ();
}
}
}
The above is a method to obtain the processing of trace information, the above method is based on the VLC exception handling mechanism written. Made some simple changes. But the above just get the exception information, if the program is installed on the user's machine, we can not get the information, the user will not be able to bring the machine to you, and then you copy the log out of it. I used to do embedded time to try, let the customer take the machine, copy the log inside, when the machine can not be networked. Now think of it all Tangled, O (∩_∩) o haha ~) in order to no longer tangle, we need a log can be sent to our server function, the following is a service information sent to our specified server function.
3. Send log via Network
Copy Code code as follows:
public class Sendcrashlog extends Asynctask<string, String, boolean>
{
public Sendcrashlog () {}
@Override
protected Boolean doinbackground (String ... params)
{
if (params[0].length () = = 0)
return false;
httpclient httpclient = new Defaulthttpclient ();
//Your server, here is just an example. Send exception information as HTTP request to server
HttpPost httppost = new HttpPost ("http://www.mythou/getlog.php");
//Here the relevant exception information to the HTTP POST request data parameter
try {
list<namevaluepair> namevaluepairs = new arraylist<namevaluepair> (1);
Namevaluepairs.add (New Basicnamevaluepair ("model", params[0));
Namevaluepairs.add (New Basicnamevaluepair ("Device", params[1));
httppost.setentity (New Urlencodedformentity (Namevaluepairs));
//Send related request information
Httpclient.execute (HttpPost);
} catch (Clientprotocolexception e) {
E.printstacktrace ();
return false;
} catch (IOException e) {
E.printstacktrace ();
return false;
}
log.d (TAG, "Device model sent.");
return true;
}
@Override
protected void OnPostExecute (Boolean result) {
}
}
Above is the use of the asynchronous task in my last article, in which we wrote a service to send an HTTP request to send the relevant exception information to the server we specified. This requires your server to parse the HTTP request sent, this is not very difficult, generally do the web people know how to do. In the exception handling above, call the sending method here:
Copy Code code as follows:
Sendcrashlogsendlog = new Sendcrashlog ();
//Just the exception information string
Sendlog. Execute (stacktrace);
The above method can send exception information to the designated server, can also track the customer's use of software, convenient for us to modify the program problem. Of course, this information collection generally have privacy and background traffic problems, this need to do a little hint in the program, so as to avoid the infamy of rogue software.