Android capture program exits unexpectedly

Source: Internet
Author: User

Android capture program exits unexpectedly

Today, I saw a CrashHandler class in Thunder animation. I guess it is a crash processing class. Let's take a look. By the way.

The Android system "abnormal exit of the program" has a negative impact on the user experience of the application. You can inherit the UncaughtExceptionHandler class to capture exceptions during application running and give friendly prompts. Use the Thread. setDefaultUncaughtExceptionHandler () method to set the exception handling class to the Thread.

 

Code:

 

public class CrashHandler implements UncaughtExceptionHandler {    private static final Logger LOG = Logger.getLogger(CrashHandler.class);    private final Application mApplication;    private Handler mUIHandler;    private Thread mUiThread;    public CrashHandler(Application app) {        mApplication = app;        mUIHandler = new Handler();        mUiThread = Thread.currentThread();    }    @Override    public void uncaughtException(Thread thread, Throwable e) {        LOG.error(e);        Throwable cause = e.getCause();        while (cause != null) {            LOG.error(cause);            cause = cause.getCause();        }        writeCrashInfoToFile(e);        if (Thread.currentThread() != mUiThread) {            mUIHandler.post(new Runnable() {                @Override                public void run() {                    mApplication.onTerminate();                }            });        } else {            mApplication.onTerminate();        }    }    private void writeCrashInfoToFile(Throwable t) {        StringWriter sw = new StringWriter();        PrintWriter pw = new PrintWriter(sw);        t.printStackTrace(pw);        Throwable cause = t.getCause();        while (cause != null) {            cause.printStackTrace(pw);            cause = cause.getCause();        }        String crashInfo = sw.toString();        pw.close();        try {            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {                File file = mApplication.getApplicationContext().getExternalCacheDir();                if (file != null) {                    file = FileUtils.getFile(file, crash);                    file.mkdirs();                    FileUtils.writeStringToFile(FileUtils.getFile(file, crash.log), crashInfo);                }            }        } catch (IOException e) {            LOG.warn(e);        }    }}
 

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.