Android error messages are captured and sent to the server

Source: Internet
Author: User

Debugging in Android project development is very important. The following is a record from the previous articles:

The biggest headache for programmers is Bug and debug. This debug lasted 20 days, and I made a failover. Tired, because of Android compatibility, different mobile phones may have different bugs, and it is difficult to reproduce, So I went online and found a similar error log to save it to the file and then upload it to the server, the source code is also shared. I did not add the code uploaded to the server. I believe everyone has ready-made code.

First, let's talk about the principle. Like the custom exception capture of javaee, the error is always thrown up and then processed at the top. The exception message can be obtained and saved.

The exception capture class is as follows.

<PRE name = "code" class = "Java"> Import Android. App. Application;

Public class app extends application {

@ Override
Public void oncreate (){
Super. oncreate ();
Crashhandler = crashhandler. getinstance ();
// Register crashhandler
Crashhandler. INIT (getapplicationcontext ());
}
} </PRE>

Set this app in androidmanifest. xml:
<PRE name = "code" class = "Java"> & lt ;? XML version = "1.0" encoding = "UTF-8 "? & Gt;
& Lt; manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. yangguangfu. uncaught" Android: versioncode = "1"
Android: versionname = "1.0" & gt;
& Lt; application Android: Name = "com. yangguangfu. uncaught. app"
Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name" & gt;
& Lt; Activity Android: Name = ". exceptionhandlerdome" Android: Label = "@ string/app_name" & gt;

& Lt; intent-filter & gt;
& Lt; Action Android: Name = "android. Intent. Action. Main"/& gt;
& Lt; Category Android: Name = "android. Intent. Category. launcher"/& gt;
& Lt;/intent-filter & gt;
& Lt;/activity & gt;
& Lt;/Application & gt;
& Lt;/manifest & gt; </PRE>

The crashhandler class is as follows:
<PRE name = "code" class = "Java"> Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Lang. thread. uncaughtexceptionhandler;

Import Android. content. context;
Import Android. OS. environment;
Import Android. OS. logoff;

/**
* @ Author a fu uniformly captures exceptions in the application and stores them in the file for the next re-opening.
*/
Public class crashhandler implements uncaughtexceptionhandler {
/**
* Whether to enable log output, enable it in debug state, and disable it in release state to prompt Program Performance
**/
Public static final Boolean DEBUG = true;
/** Default uncaughtexception processing class */
Private thread. uncaughtexceptionhandler mdefaulthandler;
/** Crashhandler instance */
Private Static final crashhandler instance = new crashhandler ();

/** Context object of the Program */
// Private context mcontext;
/** Ensure that there is only one crashhandler instance */
Private crashhandler (){
}

/** Obtain the crashhandler instance. Singleton mode */
Public static crashhandler getinstance (){
Return instance;
}

/**
* Initialize and register the context object to obtain the system's default uncaughtexception processor. Set the crashhandler as the program's default processor.
*
* @ Param CTX
*/
Public void Init (context CTX ){
// Mcontext = CTX;
Mdefaulthandler = thread. getdefaultuncaughtexceptionhandler ();
Thread. setdefaultuncaughtexceptionhandler (this );
}

/**
* When uncaughtexception occurs, it is transferred to this function for processing.
*/
Public void uncaughtexception (thread, throwable ex ){
If (! Handleexception (Ex) & amp; mdefaulthandler! = NULL ){
// If the user does not handle the exception, the system's default exception processor will handle the exception.
Mdefaulthandler. uncaughtexception (thread, ex );
} Else {// If the exception is handled by yourself, the error dialog box is not displayed, and you need to exit the app manually.
Try {
Thread. Sleep (3000 );
} Catch (interruptedexception e ){
}
Android. OS. process. killprocess (Android. OS. process. mypid ());
System. Exit (10 );
}
}

/**
* Custom error handling, collection of error information, and sending of error reports are all completed here. developers can customize the exception handling logic based on their own situations.
*
* @ Return true indicates that the exception is handled and no exception is thrown up,
* False indicates that the exception is not handled (the log information can be stored) and then handed over to the upper layer (here the Exception Processing of the system is implemented) for processing,
* Simply put, true does not bring up the error prompt box, and false does.
*/
Private Boolean handleexception (final throwable ex ){
If (EX = NULL ){
Return false;
}
// Final string MSG = ex. getlocalizedmessage ();
Final stacktraceelement [] stack = ex. getstacktrace ();
Final string message = ex. getmessage ();
// Use toast to display exception information
New thread (){
@ Override
Public void run (){
Logoff. Prepare ();
// Toast. maketext (mcontext, "program error:" + message,
// Toast. length_long). Show ();
// You can create only one file, append all the files in it, and then send them. This leads to duplicate information, which is not recommended by individuals.
String filename = "crash-" + system. currenttimemillis ()
+ ". Log ";
File file = new file (environment. getexternalstoragedirectory (),
Filename );
Try {
Fileoutputstream Fos = new fileoutputstream (file, true );
FOS. Write (message. getbytes ());
For (INT I = 0; I & lt; stack. length; I ++ ){
FOS. Write (stack [I]. tostring (). getbytes ());
}
FOS. Flush ();
FOS. Close ();
} Catch (exception e ){
}
Logoff. Loop ();
}

}. Start ();
Return false;
}

// Todo uses http post to send the Error Report to the server.
// Private void postreport (File file ){
// You can also send the version of the app, the model of the mobile phone, and other information to the server during upload,
// Android compatibility is well known, so possible errors may not be reported on every mobile phone, but it is better to debug it in a targeted manner.
//}
} </PRE>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.