Friday, July 29, 2011

How to create a thread in android without hurting the UI

Sometimes when we want to do something out of the ordinary like pushing something to the server while user is playing a game or checking emails we create Threads. If you do not set the thread priority to low this might hurt the UI performance.

Correct way to do is to call setPriority in the thread class.

SomeTaskThread someTaskThread = new SomeTaskThread();
someTaskThread.setPriority(Thread.NORM_PRIORITY-1); //Make the background thread low priority. This way it will not affect the UI performance
someTaskThread.start();

or you can use AsyncTask class.

Monday, July 18, 2011

Unable to find instrumentation target package ? duh!

Today I was trying to write some test cases after i got inspired by this link

Problem was I was keep getting "Unable to find instrumentation target package" Error and not sure how to fix it. Well there are few ways to fix this problem. Easy way out is:

1.Double click on the AndroidManifest.xml

Delete any "instrumentation" tags

2.Goto Instrumentation Tab
3.Click Add
4.Select Instrumentation
5. Select the name and target package.

This should work. Trying to add the Instrumentation manually is a nightmare.

Hope this will help to someone else

Sunday, July 3, 2011

How do I investigate an Application Not Responding (ANR) in Android?

Today I got an ANR error and it's really annoying because you know you have done something wrong and not sure where to look for to find out the problem.

When I googled, I found this on StackOverFlow.

"An ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further GUI events in the application, and thus throws up an ANR dialog." - From Stackoverflow

so, how to diagnose the problem, it's easy,

adb shell cat /data/anr/traces.txt

will drump all the appplication not responding traces to the command prompt. (Make sure to change the command prompt buffer size otherwise you can not see the full log out)