Saturday, June 6, 2015

Android example: pass string between threads, in Bundler

This example show how to pass string between Threads, in Bundle, via Handler/Message.


com.example.androidthreadlooperhandler.MainActivity.java
package com.example.androidthreadlooperhandler;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    static final String KEY_STRING_1 = "KEY_STRING_TO_THREAD";
    static final String KEY_STRING_2 = "KEY_STRING_TO_UI";

    public UiHandler uiHandler;

    EditText editText;
    Button btn;
    TextView textInfo;

    MyThread myThread;

    private class UiHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            // ...Run in UI Thread
            String strUi = msg.getData().getString(KEY_STRING_2, "no string received from THREAD");
            textInfo.setText(strUi);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText)findViewById(R.id.edittext);
        btn = (Button)findViewById(R.id.button);
        textInfo = (TextView)findViewById(R.id.info);

        btn.setOnClickListener(btnOnClickListener);

        uiHandler = new UiHandler();
        myThread = new MyThread(uiHandler);
        myThread.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //stop and quit the background Thread
        myThread.handler.getLooper().quit();
    }

    View.OnClickListener btnOnClickListener =
        new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                if(myThread.handler != null){
                    String str = editText.getText().toString();

                    Bundle bundle = new Bundle();
                    bundle.putString(KEY_STRING_1, str);
                    Message message = myThread.handler.obtainMessage();
                    message.setData(bundle);
                    myThread.handler.sendMessage(message);
                }
            }
        };

    private class MyThread extends Thread{

        public Handler handler;
        private UiHandler callbackHandler;

        MyThread(UiHandler handler){
            callbackHandler = handler;
        }

        public void run(){
            Looper.prepare();
            handler = new MyHandler();
            Looper.loop();
        }

        private class MyHandler extends Handler{
            @Override
            public void handleMessage(Message msg) {
                // ...Run in background

                String str = msg.getData().getString(KEY_STRING_1, "no string received from UI");
                String strRev = new StringBuilder(str).reverse().toString();

                Bundle bundleThread = new Bundle();
                bundleThread.putString(KEY_STRING_2, strRev);
                Message messageThread = callbackHandler.obtainMessage();
                messageThread.setData(bundleThread);
                callbackHandler.sendMessage(messageThread);

            }
        }
    }
}


layout/activity_main.xml
<LinearLayout 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"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="android-er.blogspot.com"/>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1"/>

    <TextView
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>


No comments:

Post a Comment