Threads in Android
Threads in Android
Rules:
• Activity.runOnUiThread(Runnable)
• View.post(Runnable)
• View.postDelayed(Runnable, long)
runOnUiThread
• Runs the specified action on the UI thread. If the
current thread is the UI thread, then the action is
executed immediately. If the current thread is not
the UI thread, the action is posted to the event
queue of the UI thread.
• runOnUiThread(new Runnable() {
@Override
public void run() {
videoView.start();
}
});
View.post(Runable)
• Causes the Runnable to be added to the message
queue. The runnable will be run on the UI thread.
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
final Bitmap b = loadImageFromNetwork("http://example.com/image.png");
mImageView.post(new Runnable() {
public void run() {
mImageView.setImageBitmap(bitmap);
}
});
}
}).start();
}