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/startasynctask"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start AsyncTask" />
<Button
android:id="@+id/stopasynctask"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop AsyncTask" />
<TextView
android:id="@+id/markingrunning"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
java源代碼
package com.AndroidAsyncTask;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidAsyncTask extends Activity {
Button buttonStartAsyncTask, buttonStopAsyncTask;
TextView tvRunningMark;
boolean bRunning;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStartAsyncTask = (Button)findViewById(R.id.startasynctask);
buttonStopAsyncTask = (Button)findViewById(R.id.stopasynctask);
tvRunningMark = (TextView)findViewById(R.id.markingrunning);
buttonStartAsyncTask.setEnabled(true);
buttonStopAsyncTask.setEnabled(false);
buttonStartAsyncTask.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bRunning = true;
new RunningAsyncTask().execute();
}});
buttonStopAsyncTask.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bRunning = false;
}});
}
class RunningAsyncTask extends AsyncTask<Void, String, Void>{
private String strRunningMark;
private String rotateMarking(String marking){
if (marking=="-"){
return("\\");
}
else if (marking=="\\"){
return("|");
}
else if (marking=="|"){
return("/");
}
else{
return("-");
}
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Toast.makeText(AndroidAsyncTask.this, "onPostExecute", Toast.LENGTH_LONG).show();
buttonStartAsyncTask.setEnabled(true);
buttonStopAsyncTask.setEnabled(false);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
strRunningMark = "-";
Toast.makeText(AndroidAsyncTask.this, "onPreExecute", Toast.LENGTH_LONG).show();
buttonStartAsyncTask.setEnabled(false);
buttonStopAsyncTask.setEnabled(true);
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
//tvRunningMark.setText(strRunningMark);
tvRunningMark.setText(values[0]);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
while(bRunning){
strRunningMark = rotateMarking(strRunningMark);
publishProgress(strRunningMark);
SystemClock.sleep(100);
}
return null;
}
}
}
注意的是程序中, onPreExecute(), onPostExecute(Void result)和onProgressUpdate(String... values)都是運行在UI線程, 可以直接改變UI項目. 而doInBackground(Void... params)便在後台操作執行任務.
相關文章:
- Runnable
- 線程 (Thread)
沒有留言:
發佈留言