i just want to know is there any way to get ANR notifications either through triggering any event functions or atleast catch it .despite running long process in separate thread there are possibities of ANR.so all i want to do is when app detects ANR it has to handle some functions automatically.please assist me how to achieve this.thank you.
Asked
Active
Viewed 1,840 times
1
-
1"despite running long process in separate thread there are possibities of ANR" -- then fix the bugs in your app. – CommonsWare Aug 03 '12 at 13:25
-
I think that [my answer to this question](http://stackoverflow.com/a/17883242/1269640) can help you – Salomon BRYS Jul 26 '13 at 13:57
1 Answers
0
you could use a service (preferably a foreground service) that listens to the logs (using a thread) , and if there is a log that indicate the ANR , handle it.
here's a small sample of an app that causes ANR:
...
findViewById(R.id.button).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(final View v)
{
try
{
Thread.sleep(10000);
}
catch(final InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
...
here's the log that i got from the logcat when i got the ANR:
08-03 13:02:37.746: E/ActivityManager(158): ANR in com.example.anr (com.example.anr/.MainActivity)
08-03 13:02:37.746: E/ActivityManager(158): Reason: keyDispatchingTimedOut
08-03 13:02:37.746: E/ActivityManager(158): Load: 6.19 / 2.37 / 0.86
08-03 13:02:37.746: E/ActivityManager(158): CPU usage from 5598ms to 0ms ago:
08-03 13:02:37.746: E/ActivityManager(158): 2.6% 158/system_server: 2.5% user + 0.1% kernel / faults: 86 minor
08-03 13:02:37.746: E/ActivityManager(158): 0.5% 298/com.android.phone: 0.3% user + 0.1% kernel / faults: 15 minor
08-03 13:02:37.746: E/ActivityManager(158): 0% 35/rild: 0% user + 0% kernel
08-03 13:02:37.746: E/ActivityManager(158): 4.6% TOTAL: 3.9% user + 0.6% kernel
08-03 13:02:37.746: E/ActivityManager(158): CPU usage from 2029ms to 2654ms later:
08-03 13:02:37.746: E/ActivityManager(158): 11% 158/system_server: 4.8% user + 6.4% kernel / faults: 2 minor
08-03 13:02:37.746: E/ActivityManager(158): 11% 192/InputDispatcher: 4.8% user + 6.4% kernel
08-03 13:02:37.746: E/ActivityManager(158): 1.6% 163/Compiler: 1.6% user + 0% kernel
08-03 13:02:37.746: E/ActivityManager(158): 1.6% 193/InputReader: 0% user + 1.6% kernel
08-03 13:02:37.746: E/ActivityManager(158): 18% TOTAL: 9.3% user + 9.3% kernel
so , yes , i think it's possible.

android developer
- 114,585
- 152
- 739
- 1,270
-
1It will not be possible as of Jelly Bean, as you are not be able to read the logs from other processes or the OS. Besides, reading the logs is not part of the Android SDK. – CommonsWare Aug 03 '12 at 13:24
-
correct.since i don't have JB , i can't see if there are any workarounds for this. they said in the google IO that even though now it won't work , it's ok to read the logs of your own app , so i don't know if such a thing is considered to be inside the app (i guess it's not).anyway , this solution is a workaround from the start . it should work for about 99% of the devices currently . – android developer Aug 03 '12 at 13:59