因為只有UI線程可以修改用戶界面, 因此我們必須通過消息(Message)和處理(Handler), 以顯示我們的線程狀態.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/newthread"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New a Thread" />
<Button
android:id="@+id/stoprunning"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop running" />
<Button
android:id="@+id/join"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="join" />
<Button
android:id="@+id/getstate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="getState()" />
<TextView
android:id="@+id/markingrunning"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
AndroidThread.java
package com.AndroidThread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidThread extends Activity {
String strRunning = "-";
TextView tvMarkingrunning;
Button buttonNewThread, buttonStopRunning, buttonJoin, buttonGetState;
MyThread myThread;
Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
strRunning = rotateMarking(strRunning);
tvMarkingrunning.setText("running: " + strRunning);
}
};
public class MyThread extends Thread {
private boolean running = false;
public void setRunning(boolean b){
running = b;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(running){
try {
Thread.sleep(500);
myHandler.sendMessage(myHandler.obtainMessage());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private String rotateMarking(String marking){
if (marking=="-"){
return("\\");
}
else if (marking=="\\"){
return("|");
}
else if (marking=="|"){
return("/");
}
else{
return("-");
}
}
private void setButtonsEnable(boolean enableNewThread,
boolean enableStopRunning,
boolean enableJoin){
buttonNewThread.setEnabled(enableNewThread);
buttonStopRunning.setEnabled(enableStopRunning);
buttonJoin.setEnabled(enableJoin);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvMarkingrunning = (TextView)findViewById(R.id.markingrunning);
buttonNewThread = (Button)findViewById(R.id.newthread);
buttonStopRunning = (Button)findViewById(R.id.stoprunning);
buttonJoin = (Button)findViewById(R.id.join);
buttonGetState = (Button)findViewById(R.id.getstate);
buttonNewThread.setOnClickListener(buttonNewThreadOnClickListener);
buttonStopRunning.setOnClickListener(buttonStopRunningOnClickListener);
buttonJoin.setOnClickListener(buttonJoinOnClickListener);
buttonGetState.setOnClickListener(buttonGetStateOnClickListener);
setButtonsEnable(true, false, false);
}
private Button.OnClickListener buttonGetStateOnClickListener =
new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), myThread.getState().toString(), Toast.LENGTH_LONG).show();
}
};
private Button.OnClickListener buttonNewThreadOnClickListener =
new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myThread = new MyThread();
myThread.setRunning(true);
myThread.start();
Toast.makeText(getBaseContext(), myThread.getState().toString(), Toast.LENGTH_LONG).show();
setButtonsEnable(false, true, true);
}
};
private Button.OnClickListener buttonStopRunningOnClickListener =
new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myThread.setRunning(false);
Toast.makeText(getBaseContext(), myThread.getState().toString(), Toast.LENGTH_LONG).show();
setButtonsEnable(true, false, false);
}
};
private Button.OnClickListener buttonJoinOnClickListener =
new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
boolean retry = true;
myThread.setRunning(false);
while(retry){
try {
myThread.join();
retry = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(getBaseContext(), myThread.getState().toString(), Toast.LENGTH_LONG).show();
setButtonsEnable(true, false, false);
}
};
}
相關文章:
- Runnable
- 異步任務(AsyncTask)
沒有留言:
發佈留言