Threads - UI thread and non-UI threads!!!

10 views
Skip to first unread message

Rajeev

unread,
Dec 30, 2012, 6:37:42 AM12/30/12
to winter-training...@googlegroups.com
Read the following link :developer reference

A highlights of it are given below:

Inside a Thread (which is a non-UI thread), we are not allowed to change the UI, e.g.
changing the text of a TextView, changing the bitmap of some ImageView etc.

So we have some functions, using which we can place actions onto the UI thread from a
non-UI thread.
They are runUiOnThread, post, postDelayed.

How to use:
Wrong Code:
       Thread th = new Thread(){
        public void run(){
        try{
        txt.append(".");
        }
        catch(Exception e){
        e.printStackTrace();
        }
        }
       };
Replace bold line with:
a)      runOnUiThread(new Runnable(){
@Override
public void run() {
       txt.append(".");
}
});
This will place this action on the UI thread.

b)     post(new Runnable(){
@Override
public void run() {
        txt.append(".");
}
});
This will place this action on the UI thread and if the placing is successful, it returns true.

c)     postDelayed(new Runnable(){
@Override
public void run() {
        txt.append(".");
}
}, 1000);
This will place this action on the UI thread after 1 second.

Async task is required for larger actions that can affect time.
This is not a basic concept, so you may wish to omit it.

Rajeev

unread,
Dec 30, 2012, 6:41:53 AM12/30/12
to winter-training...@googlegroups.com
Sorry, there is an error, 
Async task is used for shorter actions so that it does not affect other actions on the UI thread.
Reply all
Reply to author
Forward
0 new messages