android catches system exceptions and uploads the log implementation
- 2020-05-17 06:24:56
- OfStack
When working on a project, it is common to throw out error-exploiting exceptions so that you can troubleshoot errors by using the exceptions thrown by your phone during development. However, when the program has been developed and the version is stable and needs to go online, UncaughtExceptionHandler can be used to catch global exceptions and handle them in order to avoid throwing exceptions and affecting users' feelings. For example, we can get the time when the exception is thrown, the hardware information of the phone, the wrong stack information, and then send all the obtained information to the server, or to the specified mail, so as to modify bug in time.
Example:
The custom exception class implements the UncaughtExceptionHandler interface. When an exception occurs on a page, the method uncaughtException will be called. We can get the exception information, time, etc. in this method, and then send the information to the server we specified
/**
* The custom of Exception handling class , To achieve the UncaughtExceptionHandler interface
* @author Administrator
*
*/
public class MyCrashHandler implements UncaughtExceptionHandler {
// The demand is Whole application only 1 a MyCrash-Handler
private static MyCrashHandler myCrashHandler ;
private Context 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 context,DoubanService service){
this.context = context;
this.service = service;
}
public void uncaughtException(Thread arg0, Throwable arg1) {
System.out.println(" The program hangs ");
// 1. Gets the version number of the current program . Version of the id
String versioninfo = getVersionInfo();
// 2. Get the hardware information of the phone .
String mobileInfo = getMobileInfo();
// 3. Put the wrong stack information To get out
String errorinfo = getErrorInfo(arg1);
// 4. Take all the information And the time of the message Submit 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 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 phone
* @return
*/
private String getMobileInfo() {
StringBuffer sb = new StringBuffer();
// Get the hardware information of the system through reflection
try {
Field[] fields = Build.class.getDeclaredFields();
for(Field field: fields){
// Violence reflected , Get 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 version information of the phone
* @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 an Application instance to register MyCrashHandler to the entire application, create the service and deliver it:
/**
* The whole (app) The 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";
// Access to the service
DoubanService myService = new DoubanService(" My little bean ", apiKey,
secret);
myService.setAccessToken("1fa4e5be0f808a0b5eeeb13a2e819e21", "56a622c1138dbfce");
MyCrashHandler handler = MyCrashHandler.getInstance();
handler.init(getApplicationContext(),myService);
Thread.setDefaultUncaughtExceptionHandler(handler);
}
}