2010年6月7日星期一

Runnable

前文線程(Thread)展示了如何通過擴展線程(extends Thread)實現一個新類並重寫其的run()方法來創建一個線程.

現在來看看創建線程的另一種方法: 實現一個線程類(Thread class)的對象, 並在實現時提供一個Runnable對象.

main.xml跟前文線程(Thread)的一樣.

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;

Thread myThread;
private boolean running = false;

Handler myHandler = new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
strRunning = rotateMarking(strRunning);
tvMarkingrunning.setText("running: " + strRunning);
}
};

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 Thread(new Runnable(){

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();
}
}
}
});

setRunning(true);
myThread.start();
Toast.makeText(getBaseContext(), myThread.getState().toString(), Toast.LENGTH_LONG).show();
setButtonsEnable(false, true, true);
}
};

private void setRunning(boolean b){
running = b;
}

private Button.OnClickListener buttonStopRunningOnClickListener =
new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
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;
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);

}
};
}

相關文章:
- 線程(Thread)
- 異步任務(AsyncTask)

沒有留言:

發佈留言