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.