Capture of Android Global exception, save local and upload server

Source: Internet
Author: User
Tags http post

Android after the product is online in order to be able to catch exceptions in real time, you need to write an exception-capturing class that executes the Uncaughtexception method when you encounter an exception, and then you can do something about the exception.

I put the code up:

Package Com.example.spanable_textview;


Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.lang.Thread.UncaughtExceptionHandler;


Import Android.content.Context;
Import android.os.Environment;
Import Android.os.Looper;


public class Crashexception implements uncaughtexceptionhandler{


/** whether to turn on the log output, open in the debug state,
* Turn off to prompt for program performance in release state
* */
public static Final Boolean DEBUG = true;
/** system default Uncaughtexception processing class */
Private Thread.uncaughtexceptionhandler Mdefaulthandler;
/** Crashhandler Instance */
private static crashexception INSTANCE;
Context object for the/** program */
Private Context Mcontext;
/** guaranteed only one Crashhandler instance */
Private Crashexception () {}
/** get Crashhandler instance, Singleton mode */


public static Crashexception getinstance () {


if (instance==null) {
INSTANCE =new crashexception ();
}

return INSTANCE;
}


/**
* Initialize, register the context object,
* Get the system default Uncaughtexception processor,
* Set the Crashhandler as the default processor for the program
*
* @param context
*/


public void init (context context) {

Mdefaulthandler =thread.getdefaultuncaughtexceptionhandler ();

Thread.setdefaultuncaughtexceptionhandler (this);
}

/**
* When uncaughtexception occurs, it is transferred to the function to handle
*/
@Override
public void uncaughtexception (thread thread, Throwable ex) {


if (!handleexception (ex) &&mdefaulthandler!=null) {
// Let the system default exception handler handle if the user does not handle
Mdefaulthandler.uncaughtexception (thread, ex);
}else {
//
try {
Thread.Sleep (3000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Android.os.Process.killProcess (Android.os.Process.myPid ());
System.exit (0);
}
}

/**
* Custom error handling, collect error messages
* Send error reports and other operations are completed here.
* Developers can customize exception handling logic according to their own situation
* @return
* True means handling the exception, no longer throwing up the exception,
* False means that the exception is not handled (the log information can be stored) and then handed over to the upper (here to the system's exception handling) to deal with,
* In simple terms, true will not pop up the error box, false will pop up
*/
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 () {
Looper.prepare ();
Toast.maketext (Mcontext, "program error:" + message, Toast.length_long). Show ();
You can create only one file, and then send it all to the inside append, so there will be duplicate information, personally not recommended
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 < stack.length; i++) {
Fos.write (Stack[i].tostring (). GetBytes ());
}
Fos.flush ();
Fos.close ();
} catch (Exception e) {
}
Looper.loop ();
}

}.start ();
return false;
}

TODO uses HTTP Post to send error reports to the server don't repeat it here.
private void Postreport (file file) {
When uploading, you can also send the version of the app, the model of the phone and other information sent by the server,
Android compatibility is well known, so it may be wrong not every phone will error, or targeted to debug better
//    }


}


His use is very simple, mainly write a global application class, the direct initialization of the exception capture this class is OK, I wrote a demo test come over, can be used.

http://download.csdn.net/detail/u012808234/8439833

Capture of Android Global exception, save local and upload server

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.