Why do I need a feedback crash report?
To do Android apps, try to avoid the crash of the program. Although the 0 crash is the ultimate goal of the programmer chase, the reality is that programmers can only minimize the occurrence of crash, and almost impossible to completely eliminate crash. Perhaps you think your app's robustness is almost perfect, and it's easy to withstand the test department's devil-like test, but when your app is released to the market, it might not be as fortunate to face millions or even tens of other users.
For these reasons, a general application should have a mechanism for crash feedback. The programmer can make improvements to the current version of the code based on the results of the feedback, making the next version of the release more stable.
How to feedback?
First look at how to capture the occurrence of crash.
There is an interface in Java, Uncaughtexceptionhandler, first look at the description.
static interface
|
Thread.uncaughtexceptionhandler Invokes the interface of the handler when the Thread terminates abruptly because of an uncaught exception. |
Then look at one of the methods in the thread class.
static void
|
Setdefaultuncaughtexceptionhandler ( Thread.uncaughtexceptionhandler eh) Sets the default handler that is called when a thread is abruptly terminated because it did not catch an exception, and no other handlers are defined for the thread. |
By looking at these APIs, you know that we need to implement such an interface and then set up handlers in the main thread of the program.
Look at the following interface implementations.
- Package com.arui.framework.android.exception;
- Import Java.lang.Thread.UncaughtExceptionHandler;
- Import Android.content.Context;
- /**
- * Default exception handler for all activities.
- *
- * @author http://blog.csdn.net/arui319
- * @version 2011/12/01
- *
- */
- Public class Defaultexceptionhandler implements Uncaughtexceptionhandler {
- Private Context act = null;
- Public Defaultexceptionhandler (Context Act) {
- this.act = Act;
- }
- @Override
- public void uncaughtexception (thread thread, Throwable ex) {
- //Collect exception information and send to server
- Sendcrashreport (ex);
- //Wait half a second
- try {
- Thread.Sleep (500);
- } catch (Interruptedexception e) {
- //
- }
- //Handling Exceptions
- HandleException ();
- }
- private void Sendcrashreport (Throwable ex) {
- StringBuffer exceptionstr = new StringBuffer ();
- Exceptionstr.append (Ex.getmessage ());
- stacktraceelement[] elements = Ex.getstacktrace ();
- For (int i = 0; i < elements.length; i++) {
- Exceptionstr.append (Elements[i].tostring ());
- }
- //todo
- //Send the collected crash information to the server
- }
- private void HandleException () {
- //todo
- //The exception can be handled here.
- //such as prompting the user program crashes.
- //For example, record important information and try to restore the scene.
- //or simply record important information and kill the program directly.
- }
- }
Add the following code to the OnCreate (Bundle savedinstancestate) method of the main activity.
- Thread.setdefaultuncaughtexceptionhandler (new Defaultexceptionhandler (
- This.getapplicationcontext ()));
How do I send it to the server?
This different project group will have different ways, specifically not discussed here. It should be recalled that, in addition to sending the specific information of the exception to the server, at least the version information needs to be sent so that the programmer can determine which version of the exception information on the server appears. In addition to the version information, may also need the Phone SDK version, screen resolution, phone model and so on information, with this information, you can more comprehensive understanding of the exception information.
More instructions.
You only need to set the exception handling class once in the main activity, and you don't need to set it in all acitivity.
Personal feeling after the occurrence of crash, the resumption of the site continued to run the significance of little. After crash, the operation of the program is unpredictable, with a mistake, to make up for another error, itself will lead to more errors. It is advisable to try to avoid the occurrence of crash more reasonable.
---------------------------------------------------------------------------
GL (arui319)
http://blog.csdn.net/arui319
< This article can be reproduced, but please keep the above author information. Thank you. >
---------------------------------------------------------------------------
How Android Apps feedback crash reports