Tuesday, October 4, 2016

Update UI Thread by Worker Thread using Handler and Messages

Hi .... Dear All ....  Today I am uploading a superb example of Multi-threading. 
In this example i have one Main Thread / UI Thread and a worker thread.

Worker thread updates the status on UI thread by using Handler and Messenger.

Handler ???? 

 Handler is class it's  object is associated  with the thread in which it is created. 
 It provides a way to send data to this thread.
 A Handler is particular useful if you  want to post multiple times data to the main thread.

Message 

•       A message is an object that contains a description and arbitrary data object that can be sent to a Handler.
•       Create by calling Message.obtain() or Handler.obtainMessage() methods.
See the example
A. Output will be like this 

B. Layout files are 
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

     tools:context="com.exam.ravi.servicedemo.MessangerHandler">
    <ProgressBar

        style="?android:attr/progressBarStyleHorizontal"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/progressBar"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true"

        android:layout_alignParentStart="true"

        android:layout_alignParentEnd="true"

        android:indeterminate="false"

        android:max="100"

        android:progress="0"/>

    <TextView

        android:text="TextView"

        android:textSize="20dp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/textView"

        android:layout_below="@+id/progressBar"

        android:layout_centerHorizontal="true" />
</RelativeLayout>
C. Java code is as 
package com.exam.ravi.servicedemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MessangerHandler extends AppCompatActivity {

    Thread thread;
    Handler handler;
    ProgressBar progressBar;
    TextView textView;

    @Override
          protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_messanger);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        textView = (TextView) findViewById(R.id.textView);
        thread = new Thread(new MyThread());
        thread.start();
        handler = new Handler() {
            @Override            public void handleMessage(Message rcvmessage) {
                super.handleMessage(rcvmessage);
                progressBar.setProgress(rcvmessage.arg1);
                textView.setText((String.valueOf(rcvmessage.arg1)) 
                                                  + "% Completed ");

            }     };    }

    class MyThread implements Runnable {

        @Override        public void run() {

            for (int i = 1; i <= 100; i++) {
                Message sendmsg = Message.obtain();
                sendmsg.arg1 = i;
                handler.sendMessage(sendmsg);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
          }
}

No comments:

Post a Comment