Exception handling methods such as Android force Close and ANR

Source: Internet
Author: User

The most common exceptions for Android apps are force close and ANR (application is not response).

For these two types of errors, the application can be related to processing.

One forceclose of these problems is to catch exceptions mainly by Thread.uncaughtexceptionhandler this class . by implementing the method Uncaughtexception in the class to implement the application after catching the exception to the relevant processing. Generally here the processing is basically placed in the application class of the application. In order to facilitate the relevant processing, I wrote a class here, we directly in the application callback can be.

New Exceptionhandler (Mcontext). Setfclistener (New Exceptionhandler.fclistener () {                        @Override public            Void Onfcdispose (Throwable paramthrowable) {                log.d (TAG, "Onfclisterner enter!!!");                New Thread () {public                    void run () {                        looper.prepare ();                        Toast.maketext (Mcontext, "APP is Force Close does what want!", Toast.length_long). Show ();                        Looper.loop ();                    }                }. Start ();            }        });

The same applies to the ANR problem, the application can also do related processing. For ANR, we can handle this. Through a watchdog to detect the main thread in real time, once the main thread is blocked, notify application to do related processing.

The main method is in the thread every time (activity is generally 5S, broadcast is generally 10S), send a messager to the main thread, so that the counter plus 1, if the point does not add 1, the main thread is blocked.

 @Override public void Run () {SetName ("|        Anr-watchdog| ");        int Lasttick;            while (!isinterrupted ()) {Lasttick = Mtick;            Muihandler.post (tickerrunnable);            try {thread.sleep (mtimeoutinterval);                } catch (Interruptedexception e) {minterruptionlistener.oninterrupted (e);            return; }//If The main thread has no handled _ticker, it is blocked.            Anr.                if (Mtick = = Lasttick) {Anrerror error;                if (mnameprefix! = null) error = Anrerror.new (Mnameprefix, mlogthreadswithoutstacktrace);                else error = anrerror.newmainonly ();                Manrlistener.onappnotresponding (Error);            return; }        }    }
Private final Runnable tickerrunnable = new Runnable () {        @Override public void Run () {            Mtick = (mtick + 1)% 10;
   }    };
Source:
Https://github.com/ouclbc/ANRandFCdispose

Exception handling methods such as Android force Close and ANR

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.