Android Example: Bi-Direction Passing Information Betwixt Ui As Well As Background Thread Using Handler/Message.

Posted on

Last instance demonstrate how to live on Thread, Handler as well as Looper inward Android. In that example, UI thread expire information to background thread using Handler/Message, background thread update UI (TextView) via runOnUiThread(). It’s modified inward this example, background thread update UI via Handler/Message also.

In principal activity, implement a custom Handler (UiHandler) as well as instantiate a object, as well as then expire it to background thread via constructor.

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.TextView;  world cast MainActivity extends ActionBarActivity {      static concluding int UIMSG3 = 3;     static concluding int UIMSG4 = 4;      world UiHandler uiHandler;      Button btn1, btn2;     TextView textInfo;      MyThread myThread;      someone cast UiHandler extends Handler{         @Override         world void handleMessage(Message msg) {             // ...Run inward UI Thread             int what = msg.what;             switch (what){                 instance UIMSG3:                     textInfo.setText("UI Message 3");                     break;                 instance UIMSG4:                     textInfo.setText("UI Message 4");                     break;             }         }     }      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         btn1 = (Button)findViewById(R.id.button1);         btn2 = (Button)findViewById(R.id.button2);         textInfo = (TextView)findViewById(R.id.info);          btn1.setOnClickListener(btnOnClickListener);         btn2.setOnClickListener(btnOnClickListener);          uiHandler = novel UiHandler();         myThread = novel MyThread(uiHandler);         myThread.start();     }      @Override     protected void onDestroy() {         super.onDestroy();          //stop as well as quit the background Thread         myThread.handler.getLooper().quit();     }      View.OnClickListener btnOnClickListener =         novel View.OnClickListener(){              @Override             world void onClick(View v) {                 if(myThread.handler != null){                     Message message;                     if(v==btn1){                         message = myThread.handler.obtainMessage(MyThread.MSG1);                     }else{                         message = myThread.handler.obtainMessage(MyThread.MSG2);                     }                     myThread.handler.sendMessage(message);                 }             }         };      someone cast MyThread extends Thread{          static concluding int MSG1 = 1;         static concluding int MSG2 = 2;          world Handler handler;         someone UiHandler callbackHandler;          MyThread(UiHandler handler){             callbackHandler = handler;         }          world void run(){             Looper.prepare();             handler = novel MyHandler();             Looper.loop();         }          someone cast MyHandler extends Handler{             @Override             world void handleMessage(Message msg) {                 // ...Run inward background                  int what = msg.what;                 switch (what){                     instance MSG1:                          //doing something...                         motility {                             Thread.sleep(2000);                         } grab (InterruptedException e) {                             e.printStackTrace();                         }                          Message callbackMessage3;                         callbackMessage3 = callbackHandler.obtainMessage(UIMSG3);                         callbackHandler.sendMessage(callbackMessage3);                          /*                         runOnUiThread(new Runnable() {                             @Override                             world void run() {                                 textInfo.setText("Message 1");                             }                         });                         */                          break;                     instance MSG2:                          //doing something...                         motility {                             Thread.sleep(2000);                         } grab (InterruptedException e) {                             e.printStackTrace();                         }                          Message callbackMessage4;                         callbackMessage4 = callbackHandler.obtainMessage(UIMSG4);                         callbackHandler.sendMessage(callbackMessage4);                          /*                         runOnUiThread(new Runnable() {                             @Override                             world void run() {                                 textInfo.setText("Message 2");                             }                         });                         */                          break;                  }             }         }     } }  

The layout file advert to in conclusion instance Thread, Handler as well as Looper inward Android.

Next:
– Android example: expire string betwixt threads, inward Bundler