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.

No comments:

Post a Comment