HandlerExampleActivity.java
package com.jing.example.nknu_.handlerexampleactivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.Random;
public class HandlerExampleActivity extends AppCompatActivity {
private final static int SHOW_PROGRESS_BAR = 1;
private final static int HIDE_PROGRESS_BAR = 0;
private BackgroundThread mBackgroundThread;
private TextView mText;
private Button mButton;
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handler_example);
mBackgroundThread = new BackgroundThread();
mBackgroundThread.start();
mText = (TextView)findViewById(R.id.textView);
mProgressBar = (ProgressBar)findViewById(R.id.progressBar);
mButton= (Button)findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBackgroundThread.doWork();
}
});
}
protected void onDestroy(){
super.onDestroy();
mBackgroundThread.exit();
}
public class BackgroundThread extends Thread {
private final static int SHOW_PROGRESS_BAR = 1;
private final static int HIDE_PROGRESS_BAR = 0;
private Handler mBackgroundHandler;
public void run() {
Looper.prepare();
mBackgroundHandler = new Handler();
Looper.loop();
}
public void doWork() {
mBackgroundHandler.post(new Runnable() {
@Override
public void run() {
System.out.println("Thread ID = " + Thread.currentThread().getId());
Message uiMsg = mUiHandler.obtainMessage(SHOW_PROGRESS_BAR, 0, 0, null);
mUiHandler.sendMessage(uiMsg);
Random r = new Random();
int randomInt = r.nextInt(5000);
SystemClock.sleep(randomInt);
uiMsg = mUiHandler.obtainMessage(HIDE_PROGRESS_BAR, randomInt, 0, null);
mUiHandler.sendMessage(uiMsg);
}
});
}
public void exit(){
mBackgroundHandler.getLooper().quit();
}
private final Handler mUiHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_PROGRESS_BAR:
mProgressBar.setVisibility(View.VISIBLE);
break;
case HIDE_PROGRESS_BAR:
mText.setText(String.valueOf(msg.arg1));
mProgressBar.setVisibility(View.INVISIBLE);
break;
}
}
};
}
}
content_handler_example.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.jing.example.nknu_.handlerexampleactivity.HandlerExampleActivity"
tools:showIn="@layout/activity_handler_example">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="205dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/button"
android:layout_marginTop="102dp" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/textView" />
</RelativeLayout>