Thread in Android: An Example

StrictMode is most commonly used to catch accidental disk or network access on the application’s main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application’s main thread (UI thread) responsive, you also prevent ANR (Application Not Responding) dialogs from being shown to users.

Network requests being made on UI thread may cause your application UI to stop responding to user interaction. Therefore it is best to keep off your network request operations from main UI thread. Trying to force doing a network requests on main UI thread may raise NetworkOnMainThreadException exception.

For example when you want to execute some code in other thread, simply a code inside a thread object runnable like below:

public void onClick(View v) {
  new Thread(new Runnable() {
    public void run() {
      // put your long and time consuming codes here...
    }
  }).start();
}

But it is often that when your thread has done doing something, you need to update some text or picture on Android UI toolkit which is not thread-safe on main UI thread. This may cause problem when you try to update Android UI toolkit like TextView or EditText from your non-UI thread. A quick fix like below may simply solve the problem:

public void onClick(View v) {
  new Thread(new Runnable() {
    public void run() {
      // put your long and time consuming codes here...
      ...
      ...
      myTextView.post(new Runnable() {
        public void run() {
          myTextView.setText("Loading complete!");
        }
      });
    }
  }).start();
}

On above code, the myTextView TextView’s text is safely updated in UI thread using View’s post method on a runnable. Accessing UI thread from another thread can be achieved by:

If you frequently update Android UI toolkit (e.g. TextView) from another thread, then the above code may likely cause problem as they are difficult to read. Since Android version 1.5, there is an utility class that simplify the long running tasks communication with UI thread named AsyncTask but it must be extended to be used.

source:

http://android-developers.blogspot.in/2009/05/painless-threading.html
http://developer.android.com/reference/android/os/StrictMode.html
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

2 thoughts on “Thread in Android: An Example

  1. Thank you very much for the article! I was searching since hours for a solution for my problem, setText to Textviews from an other thread. I found the solution on your site! Thanks so much!

    Uwe!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

CAPTCHA Image

*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>