Is there a way to get notified when your app triggers an ANR (Application Not Responding)? Similar to the default exception handler?
In anticipation of the "what would you do with it" answers, just logging. Not "doing" anything.
Since the system watchdog does not warn the application, the application itself can have it's own watchdog. The steps are simple, just start a thread that loops doing the following :
I have written a small library that does exactly that and that I use with ACRA.
I hope it helps ;)
No. Unlike exceptions that occur within your process's VM that you can catch, the ANR is generated by a system watchdog, outside your VM. Google offers info on triggers and avoidance
I've been thinking about this quite a bit. You could do the following, although its pretty heavy handed. ANR's write out a threads file to a generally readable directory:
/data/anr/traces.txt
You could have a service, in a different process, poll that file periodically. If the date changes, and your app is at the top, then you probably had an ANR event.
I'm not 100% sure of the format of the file, though.
This small ANR-WatchDog thread can help you to monitor your app's thread and get notified when ANR happens.
new ANRWatchDog().setANRListener(new ANRWatchDog.ANRListener() {
@Override
public void onAppNotResponding(ANRError error) {
// Handle the error. For example, log it to HockeyApp:
ExceptionHandler.saveException(error, new CrashManager());
}
}).start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With