Worker threads are background threads. They are the threads that
are created separately, other than the UI thread. Since blocking the UI thread
is restricted according to the rule, user should run the child processes and
tasks in worker threads.
An example for creation and working of worker thread is given
below:
public void onClick(View v) {
new Thread(new
Runnable() {
public void run() {
Bitmap b = loadImageFromNetwork("http://example.com/image.png");
mImageView.setImageBitmap(b);
}
}).start();
}
In the above example code, the download operation is handled by
a second thread other than the UI thread. But the program violates the second
rule. The imageView from UI thread is manipulating from this worker thread.
According to the second rule, UI could not be accessed from
outside the UI thread. Solution for such a restriction is
runOnUiThread(Runnable) method. The main or UI thread can be accessed from
other threads using runOnUiThread(Runnable) method.
As a result, the specified runnable action passed through this
method will run on the UI thread. The action will execute immediately, if the
current thread is in the UI itself. Else the action will be posted to the event
queue.
An example code:
MainClassName.this.runOnUiThread(new Runnable() {
public void run() {
textViewObject.setText("Set this " + value from worker thread +
"!");
}
});
Only using the methods like runOnUiThread(Runnable), the program
can touch the UI views.
Handlers and Runnable
A better solution to process the messages delivered from the UI
thread is handlers. The runnable objects can also be processed using the
handlers. Runnable is a command that can be executed. It is used to run the
code in other thread.
An example that users handlers and runnable objects in worker
thread is given here;
WorkerThreadActivity.class
package com.febi.worker;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class WorkerThreadActivity
extends Activity{
ImageView image;
String
url="http://icons.iconarchive.com/icons/icons-land/sport/256/Soccer-Ball-icon.png";
/** Called when
the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void
onClick(View v){
final Handler threadHandler;
final Context cbt;
cbt = this;
threadHandler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
threadHandler.post(new Runnable() {
@Override
public void run() {
InputStream is = null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
is = con.getInputStream();
}catch(Exception e)
{
Log.i("LOGCAT Run", ""+e);
}
Bitmap bmp = BitmapFactory.decodeStream(is);
ImageView
image=(ImageView)findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
}
});
}
}).start();
AlertDialog.Builder
ab = new AlertDialog.Builder(cbt);
ab.setMessage("A message sent from UI thread");
ab.show();
}
}
main.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Click Me"
android:onClick="onClick"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Set permission for Internet in manifest file ;
<uses-permission android:name="android.permission.INTERNET"/>
Good article.. Not quite readable..
ReplyDeleteWell written article, But not good readability of the highlighted text
ReplyDelete