Handlerthread Example

Posted on

Last postal service exhibit examples of AsyncTask as well as Thread + Handler, hither is or as well as thus other version using HandlerThread.

This video exhibit how it run on Android Emulator running Android N, inward Multi-Window. The left windows running instance of HandlerThread inward this post, the correct window running Thread + Handler inward lastly post.


MainActivity.java

package com.blogspot.android_er.androidhandlerthread;  import android.os.Handler; import android.os.HandlerThread; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView;  populace course of pedagogy MainActivity extends AppCompatActivity {      someone Handler handler = novel Handler();     someone MyHandlerThread myHandlerThread;      Button btnStart;     ProgressBar progressBar;     TextView textMsg;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          progressBar = (ProgressBar)findViewById(R.id.progress);         textMsg = (TextView)findViewById(R.id.msg);         btnStart = (Button)findViewById(R.id.start);          myHandlerThread = novel MyHandlerThread("myHandlerThread");         terminal Runnable myRunnable = novel Runnable() {              @Override             populace void run() {                 for (int i = 0; i <= 10; i++) {                     try {                         Thread.sleep(1000);                     } grab (InterruptedException e) {                         e.printStackTrace();                     }                      //is accessed from inside inner class, needs to live declared terminal                     terminal int finalI = i;                     handler.post(new Runnable() {                         @Override                         populace void run() {                             progressBar.setProgress(finalI);                         }                     });                 }                  handler.post(new Runnable() {                     @Override                     populace void run() {                         textMsg.setText("finished");                     }                 });             }         };          myHandlerThread.start();         myHandlerThread.prepareHandler();          btnStart.setOnClickListener(new View.OnClickListener() {             @Override             populace void onClick(View v) {                 myHandlerThread.postTask(myRunnable);             }         });     }      @Override     protected void onDestroy() {         myHandlerThread.quit();         super.onDestroy();     }      populace course of pedagogy MyHandlerThread extends HandlerThread {          someone Handler handler;          populace MyHandlerThread(String name) {             super(name);         }          populace void postTask(Runnable task){             handler.post(task);         }          populace void prepareHandler(){             handler = novel Handler(getLooper());         }     } }  

layout/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.androidhandlerthread.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/start"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Start"/>      <ProgressBar         android:id="@+id/progress"         style="?android:attr/progressBarStyleHorizontal"         android:indeterminate="false"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:max="10"         android:progress="0"/>     <TextView         android:id="@+id/msg"         android:layout_width="match_parent"         android:layout_height="wrap_content"/> </LinearLayout>  

reference: Get a HandlerThread on your App