Android message passing sample using handler


package com.techjini.handler.sample;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class HandlerSample extends Activity {

    private static final int MSG_STOP_THREAD = 0x0001;
    private static final int MSG_STRING_OPERATION = 0x0002;
    private static final int MSG_LONG_OP_START = 0x0003;

    private static final int RESP_THREAD_READY = 0x0100;
    private static final int RESP_THREAD_FINISH = 0x0101;
    private static final int RESP_STRING_OPERATION = 0x0102;
    private static final int RESP_LONG_OP_STARTED = 0x0103;
    private static final int RESP_LONG_OP_FINISHED = 0x0104;

    private static final String RESP_KEY_1 = "RESP_KEY_1";

    private Handler uiThreadHandler;
    private Handler childThreadHandler;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        uiThreadHandler = new UIThreadHandler();
        new SampleThread().start();
        OnClickListener listener = new OnClickListener() {

            public void onClick(View v) {
                if (null != childThreadHandler) {
                    switch (v.getId()) {
                    case R.id.button1:
                        childThreadHandler
                                .sendEmptyMessage(MSG_STRING_OPERATION);
                        break;
                    case R.id.button2:

                        childThreadHandler
                                .sendEmptyMessage(MSG_LONG_OP_START);

                        break;
                    }

                }
            }
        };
        findViewById(R.id.button1).setOnClickListener(listener);
        findViewById(R.id.button2).setOnClickListener(listener);
    }

    private class SampleThread extends Thread {

        @Override
        public void run() {

            // Pause this thread so that UI thread get some time to breathe.
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // Start the looper
            Looper.prepare();
            // create instance of child thread handler
            childThreadHandler = new ChildThreadHandler();
            // intimate ui thread that child thread is ready
            uiThreadHandler.sendEmptyMessage(RESP_THREAD_READY);
            Looper.loop();
        }

    }

    // extending handler class to handle messages sent to UI thread
    private class UIThreadHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case RESP_THREAD_READY:
                Toast.makeText(HandlerSample.this, "Child thread ready",
                        Toast.LENGTH_SHORT).show();
                break;
            case RESP_THREAD_FINISH:
                Toast.makeText(HandlerSample.this, "Child thread finished",
                        Toast.LENGTH_SHORT).show();
                break;
            case RESP_STRING_OPERATION:
                Toast.makeText(HandlerSample.this, msg.obj.toString(),
                        Toast.LENGTH_SHORT).show();
                break;
            case RESP_LONG_OP_STARTED:
                ((Button) findViewById(R.id.button2)).setVisibility(View.GONE);
                Toast.makeText(HandlerSample.this, "Long operation started",
                        Toast.LENGTH_SHORT).show();
                break;
            case RESP_LONG_OP_FINISHED:
                ((Button) findViewById(R.id.button2))
                        .setVisibility(View.VISIBLE);
                Toast.makeText(
                        HandlerSample.this,
                        "After long consideration, thread thinks that you are "
                                + msg.getData().getString(RESP_KEY_1),
                        Toast.LENGTH_SHORT).show();
                break;
            }
            super.handleMessage(msg);
        }
    }

    // extending handler class to handle messages sent to child thread
    private class ChildThreadHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_STOP_THREAD:
                // end looper
                Looper.myLooper().quit();
                // send message to ui thread
                uiThreadHandler.sendEmptyMessage(RESP_THREAD_FINISH);
                break;
            case MSG_STRING_OPERATION:
                Message message = obtainMessage(RESP_STRING_OPERATION);
                message.obj = "Hello from child thread.";
                uiThreadHandler.sendMessage(message);
                break;
            case MSG_LONG_OP_START:
                uiThreadHandler.sendEmptyMessage(RESP_LONG_OP_STARTED);
                // perform long operation
                try {
                    Thread.sleep(2000);
                    // if operation done send back success
                    // response
                    Message successMessage = obtainMessage(RESP_LONG_OP_FINISHED);
                    Bundle bundle = new Bundle();
                    bundle.putString(RESP_KEY_1,
                            System.currentTimeMillis() % 2 == 1 ? "smart"
                                    : "idiot");
                    successMessage.setData(bundle);
                    uiThreadHandler.sendMessage(successMessage);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            }
            super.handleMessage(msg);
        }
    }

    @Override
    protected void onStop() {
        childThreadHandler.sendEmptyMessage(MSG_STOP_THREAD);
        super.onStop();
    }
}
Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s