Thursday, January 20, 2011

Android ANR Debug tools

From API level 9, Android provides a tool, i.e.StrictMode class to look for UI blocking issues commonly known to dev as ANR(Application Not Responding). It happens when the main EDT(Event Dispatch Thread) doesn't return in the specified time. For BroadcastReceiver, its 10 secs, 5 secs for touch events etc. Android works on Single Thread mechanism for interrupt handling and paint routines.
Using the Strict mode class, you can specify the types of blocking issues, you want to catch, i.e. disk I/O, network, logs etc.
I'm surprised even accessing the shared preferences on a UI thread is treated as a violation. The reason presented by Android is that the Shared Preferences is actually a xml file saved onto the disk. So, accessing it in the main thread is also a violation. I wonder if if there's a workaround on this. Nobody wants another thread to deal with SharedPrefs.
Otherwise, a good tool(in fact, a class) to use. Only can be tested on 2.3 platform onwards. So, its time to make good use of the emulator.
         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                 .detectNetwork()   // or .detectAll() for all detectable problems
         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
For more info, have a look at,

No comments:

Post a Comment