Advertisemen
Last post show examples of AsyncTask and Thread + Handler, here is another version using HandlerThread.
This video show how it run on Android Emulator running Android N, in Multi-Window. The left windows running example of HandlerThread in this post, the right window running Thread + Handler in last 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;
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private 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 = new MyHandlerThread("myHandlerThread");
final Runnable myRunnable = new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//is accessed from within inner class, needs to be declared final
final int finalI = i;
handler.post(new Runnable() {
@Override
public void run() {
progressBar.setProgress(finalI);
}
});
}
handler.post(new Runnable() {
@Override
public void run() {
textMsg.setText("finished");
}
});
}
};
myHandlerThread.start();
myHandlerThread.prepareHandler();
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myHandlerThread.postTask(myRunnable);
}
});
}
@Override
protected void onDestroy() {
myHandlerThread.quit();
super.onDestroy();
}
public class MyHandlerThread extends HandlerThread {
private Handler handler;
public MyHandlerThread(String name) {
super(name);
}
public void postTask(Runnable task){
handler.post(task);
}
public void prepareHandler(){
handler = new 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="http://android-er.blogspot.com/"
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
Advertisemen