Android Illustration Of Using Thread Together With Handler

Posted on
This illustration present how to run code inward background using Thread, as well as also present passing information from action to thread yesteryear calling method of thread, as well as passing information from thread to action alongside Handler.


MainActivity.java

package com.blogspot.android_er.androidthread;  import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;  populace flat MainActivity extends AppCompatActivity {      Button btnStart, btnStop, btnSend;     EditText editTextMsgToSend;     TextView textViewCntReceived, textViewMsgReceived;      MyThread myThread;     MyHandler myHandler;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         btnStart = (Button)findViewById(R.id.startthread);         btnStop = (Button)findViewById(R.id.stopthread);         btnSend = (Button)findViewById(R.id.send);         editTextMsgToSend = (EditText)findViewById(R.id.msgtosend);         textViewCntReceived = (TextView)findViewById(R.id.cntreceived);         textViewMsgReceived = (TextView)findViewById(R.id.msgreceived);          myHandler = novel MyHandler(this);          btnStart.setOnClickListener(btnStartOnClickListener);         btnStop.setOnClickListener(btnStopOnClickListener);         btnSend.setOnClickListener(btnSendOnClickListener);     }      View.OnClickListener btnStartOnClickListener =             novel View.OnClickListener() {         @Override         populace void onClick(View v) {             if(myThread != null){                 myThread.setRunning(false);             }             myThread = novel MyThread(myHandler);             myThread.start();         }     };      View.OnClickListener btnStopOnClickListener =             novel View.OnClickListener() {         @Override         populace void onClick(View v) {             if(myThread != null){                 myThread.setRunning(false);                 myThread = null;             }         }     };      View.OnClickListener btnSendOnClickListener =             novel View.OnClickListener() {         @Override         populace void onClick(View v) {             if(myThread != null){                 myThread.msgToThread(editTextMsgToSend.getText().toString());             }         }     };      someone void updateCnt(int cnt){         textViewCntReceived.setText(String.valueOf(cnt));     }      someone void updateMsg(String msg){         textViewMsgReceived.setText(msg);     }       populace static flat MyHandler extends Handler{          populace static in conclusion int UPDATE_CNT = 0;         populace static in conclusion int UPDATE_MSG = 1;         someone MainActivity parent;          populace MyHandler(MainActivity parent) {             super();             this.parent = parent;         }          @Override         populace void handleMessage(Message msg) {              switch (msg.what){                 instance UPDATE_CNT:                     int c = (int)msg.obj;                     parent.updateCnt(c);                     break;                 instance UPDATE_MSG:                     String m = (String)msg.obj;                     parent.updateMsg(m);                     break;                 default:                     super.handleMessage(msg);             }          }     } }  

MyThread.java

package com.blogspot.android_er.androidthread;  import android.os.Looper; import android.os.Message;  populace flat MyThread extends Thread{      someone int cnt;     someone boolean running;     MainActivity.MyHandler mainHandler;      populace MyThread(MainActivity.MyHandler mainHandler) {         super();         this.mainHandler = mainHandler;     }      populace void setRunning(boolean running){         this.running = running;     }      populace void msgToThread(String msgin){         String msgout = novel StringBuilder(msgin).reverse().toString();         mainHandler.sendMessage(                 Message.obtain(mainHandler,                         MainActivity.MyHandler.UPDATE_MSG, msgout));     }      @Override     populace void run() {         cnt = 0;         running = true;          String prompt;         if(Looper.myLooper() == Looper.getMainLooper()){             prompt = "myThread run inward UI Thread";         }else{             prompt = "myThread run inward NOT UI Thread";         }         mainHandler.sendMessage(                 Message.obtain(mainHandler,                         MainActivity.MyHandler.UPDATE_MSG, prompt));          piece (running){             endeavour {                 Thread.sleep(1000);                 mainHandler.sendMessage(                         Message.obtain(mainHandler,                                 MainActivity.MyHandler.UPDATE_CNT, cnt));                  cnt++;             } select stimulate of (InterruptedException e) {                 e.printStackTrace();             }         }     } }  

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <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:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidthread.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <Button         android:id="@+id/startthread"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Start Thread"/>     <Button         android:id="@+id/stopthread"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Stop Thread"/>     <EditText         android:id="@+id/msgtosend"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="msg to send..." />     <Button         android:id="@+id/send"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Send msg to thread"/>     <TextView         android:id="@+id/cntreceived"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="bold"         android:textSize="26dp"/>     <TextView         android:id="@+id/msgreceived"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:textStyle="bold"         android:textSize="20dp"/> </LinearLayout>  

 This illustration present how to run code inward background using Thread Android illustration of using Thread as well as HandlerDownload the files .

Related:
– Android illustration of using Service as well as BroadcastReceiver
– Example of IntentService as well as BroadcastReceiver