When you do a project, you often throw the error out of the way, so that you can use the exception to troubleshoot the phone when you develop it. But when the program has been developed, the version is stable and needs to be online, in order to avoid throwing an exception to affect the user's feelings, you can use Uncaughtexceptionhandler to catch the global exception and handle the exception. For example, we can get the time to throw an exception, the hardware information of the mobile phone, the wrong stack information, and then send all the information to the server, or sent to the specified message, so that the bug can be modified in a timely manner.
Example:
The custom exception class implements the Uncaughtexceptionhandler interface, and when a page gets an exception, it calls the Uncaughtexception method, where we can get exception information, time, etc. And then send the information to the server we specified
Copy Code code as follows:
/**
* 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 Mycrashhandler;
private context;
Private Doubanservice service;
Private SimpleDateFormat DataFormat = new SimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss");
1. Method of construction of privatization
Private Mycrashhandler () {
}
public static synchronized Mycrashhandler getinstance () {
if (mycrashhandler!=null) {
return mycrashhandler;
}else {
Mycrashhandler = new Mycrashhandler ();
return mycrashhandler;
}
}
public void init (context Context,doubanservice service) {
This.context = context;
This.service = Service;
}
public void Uncaughtexception (Thread arg0, Throwable arg1) {
system.out.println ("program hangs Out");
//1. Gets the version number of the current program. Version ID
string versioninfo = GetVersionInfo ();
//2. Get the hardware information of the mobile phone.
string mobileinfo = Getmobileinfo ();
//3. Get the wrong stack information
string errorinfo = GetErrorInfo (arg1);
//4. Submit all the 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 ();
 &NBSP}
//kill the current program
android.os.process.killprocess ( Android.os.Process.myPid ());
}
/**
* Get the wrong 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;
}
/**
* Get the hardware information of the mobile phone
* @return
*/
Private String Getmobileinfo () {
StringBuffer sb = new StringBuffer ();
Obtaining hardware information for a system through reflection
try {
field[] fields = Build.class.getDeclaredFields ();
for (Field field:fields) {
//violent reflection, obtaining 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 ();
 &NBSP}
return sb.tostring ();
.}
/**
* Get the 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 "version number unknown";
}
}
}
Create a application instance to register the Mycrashhandler on the entire application, create a service and deliver it:
Copy Code code as follows:
/**
* Called before initialization of the entire (APP) program
* @author Administrator
*
*/
public class Doubanapplication extends application {
Public Noteentry entry;
@Override
public void OnCreate () {
Super.oncreate ();
String Apikey = "0fab7f9aa21f39cd2f027ecfe65dad67";
String secret = "87fc1c5e99bfa5b3";
Get to Service
Doubanservice MyService = new Doubanservice ("My Adzuki bean", Apikey,
Secret);
Myservice.setaccesstoken ("1fa4e5be0f808a0b5eeeb13a2e819e21", "56A622C1138DBFCE");
Mycrashhandler handler = Mycrashhandler.getinstance ();
Handler.init (Getapplicationcontext (), myservice);
Thread.setdefaultuncaughtexceptionhandler (handler);
}
}