During project creation, errors are often thrown out using exceptions, so that you can troubleshoot the exceptions thrown by mobile phones during development. However, when the program is developed and the version is stable and needs to be released, you can use uncaughtexceptionhandler to capture and handle the global exception to avoid the impact of throwing an exception. For example, we can get the time when an exception is thrown, the hardware information of the mobile phone, and the stack information of the Error. Then, we can send all the obtained information to the server or the specified email, to promptly modify bugs.
Example:
The custom exception class implements the uncaughtexceptionhandler interface. When an exception occurs on a page, the uncaughtexception method is called. In this method, we can obtain the exception information and time, then, send the obtained information to the specified server.
/**
* The custom exception handling class implements the uncaughtexceptionhandler interface.
* @ Author Administrator
*
*/
Public class mycrashhandler implements uncaughtexceptionhandler {
// The requirement is that the entire application has only one mycrash-Handler
Private Static mycrashhandler;
Private context;
Private doubanservice service;
Private simpledateformat dataformat = new simpledateformat ("yyyy-mm-dd-hh-mm-SS ");
// 1. Privatization Construction Method
Private mycrashhandler (){
}
Public static synchronized mycrashhandler getinstance (){
If (mycrashhandler! = NULL ){
Return mycrashhandler;
} Else {
Mycrashhandler = new mycrashhandler ();
Return mycrashhandler;
}
}
Public void Init (context, doubanservice Service ){
This. Context = context;
This. Service = service;
}
Public void uncaughtexception (thread arg0, throwable arg1 ){
System. Out. println ("program crashed ");
// 1. Get the current program version number. Version ID
String versioninfo = getversioninfo ();
// 2. Obtain the hardware information of the mobile phone.
String mobileinfo = getmobileinfo ();
// 3. Get the wrong stack information
String errorinfo = geterrorinfo (arg1 );
// 4. Submit all information and information to the server
Try {
Service. createnote (New plaintextconstruct (dataformat. Format (new date ())),
New plaintextconstruct (versioninfo + mobileinfo + errorinfo), "public", "yes ");
} Catch (exception e ){
E. printstacktrace ();
}
// Kill the current program
Android. OS. process. killprocess (Android. OS. process. mypid ());
} /**
* Get error information
* @ Param arg1
* @ Return
*/
Private string geterrorinfo (throwable arg1 ){
Writer writer = new stringwriter ();
Printwriter PW = new printwriter (writer );
Arg1.printstacktrace (PW );
PW. Close ();
String error = writer. tostring ();
Return Error;
} /**
* Obtain the hardware information of the mobile phone.
* @ Return
*/
Private string getmobileinfo (){
Stringbuffer sb = new stringbuffer ();
// Obtain the system hardware information through reflection
Try { Field [] fields = build. Class. getdeclaredfields ();
For (field: fields ){
// Brute force reflection to obtain private information
Field. setaccessible (true );
String name = field. getname ();
String value = field. Get (null). tostring ();
SB. append (name + "=" + value );
SB. append ("\ n ");
}
} Catch (exception e ){
E. printstacktrace ();
}
Return sb. tostring ();
} /**
* Get the mobile phone version information
* @ Return
*/
Private string getversioninfo (){
Try {
Packagemanager PM = context. getpackagemanager ();
Packageinfo info = PM. getpackageinfo (context. getpackagename (), 0 );
Return info. versionname;
} Catch (exception e ){
E. printstacktrace ();
Return "unknown version number ";
}
}
} |
Create an application instance and register mycrashhandler to the entire application. Then, create a service and pass it through:
/**
* The entire (APP) program is called before Initialization
* @ Author Administrator
*
*/
Public class doubanapplication extends application {
Public noteentry entry;
@ Override
Public void oncreate (){
Super. oncreate ();
String apikey = "0fab7f9aa21f39cd2f027ecfe65dad67 ";
String secret = "87fc1c5e99bfa5b3 ";
// Obtain the service
Doubanservice myservice = new doubanservice ("My beans", apikey,
Secret );
Myservice. setaccesstoken ("1fa4e5be0f808a0b5eeeb13a2e819e21", "56a622c1138dbfce ");
Mycrashhandler handler = mycrashhandler. getinstance ();
Handler. INIT (getapplicationcontext (), myservice );
Thread. setdefaultuncaughtexceptionhandler (handler );
}
} |
From http://www.51testing.com/html/20/n-811320.html