Android Capture program exits unexpectedly

Source: Internet
Author: User

Today see Thunder Anime inside a Crashhandler class, I guess is the crash processing class. In a look, sure enough. Learn by the way.

The "program exception exit" of the Android system adversely affects the user experience of the app. To capture application run-time exceptions and give friendly hints, you can inherit the Uncaughtexceptionhandler class to handle. The exception-handling class is set to the thread by using the Thread.setdefaultuncaughtexceptionhandler () method.


Code:

public class Crashhandler implements Uncaughtexceptionhandler {private static final Logger LOG = Logger.getlogger (Cras    Hhandler.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); }    }}
Two references that can be seen:

Http://blog.csdn.net/hehe9737/article/details/7662123

http://blog.csdn.net/wangbole/article/details/8161524

Code is their own, although simple, I am the original bar. Otherwise, it's really hard to put on the force.

Android Capture program exits unexpectedly

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.