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