Android Exception capture processing

Source: Internet
Author: User

Inherit the Uncaughtexceptionhandler interface, override the method, and do the processing

Package com.example.exp;

Import Java.lang.Thread.UncaughtExceptionHandler;

Import com.example.threadtest.MainActivity;

Import Android.app.AlertDialog;
Import android.app.Application;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnClickListener;
Import android.content.Intent;
Import Android.os.Looper;
Import Android.util.Log;

public class Crashhandler implements Uncaughtexceptionhandler {

Public static final String TAG = "Crashhandler";
private static Crashhandler INSTANCE = new Crashhandler ();
private application Mcontext;
private Thread.uncaughtexceptionhandler Mdefaulthandler;

Private Crashhandler () {
}

Public static Crashhandler getinstance () {
return INSTANCE;
}

Public void init (Application ctx) {
mcontext = CTX;
Mdefaulthandler = Thread.getdefaultuncaughtexceptionhandler ();
Thread.setdefaultuncaughtexceptionhandler (this);
}

@Override
Public void Uncaughtexception (thread thread, Throwable ex) {
//if (!handleexception (ex) && Mdefaulthandler! = null) {
//Mdefaulthandler.uncaughtexception (thread, ex);
//} else {
//Android.os.Process.killProcess (Android.os.Process.myPid ());
//System.exit (ten);
// }
if (!handleexception (ex) && Mdefaulthandler! = null) {
//If the user does not handle it, let the system default exception handler handle
mdefaulthandler.uncaughtexception (thread, ex);
} else {
try {
Thread.Sleep (+);
} catch (Interruptedexception e) {
log.e (TAG, "error:", e);
}
//Restart the program, commenting the exit program above
Intent Intent = new Intent ();
Intent.setclass (mcontext,mainactivity.class);
intent.addflags (intent.flag_activity_new_task);
mcontext.startactivity (intent);
android.os.Process.killProcess (Android.os.Process.myPid ());
}

}

/**
* Custom error handling, collect error messages, send error reports, and so on. Developers can customize exception handling logic to their own situation
*
* @param ex
* @return true: Returns False if the exception information is processed;
*/
Private Boolean handleexception (Throwable ex) {
if (ex = = null) {
return true;
}
//todo

return true;
}
}

Android Exception capture processing

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.