<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_start"
android:text="開始"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_stop"
android:text="停止"
/>
<ProgressBar
android:id="@+id/main_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
</LinearLayout>
package com.example.demo07;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button button_start, button_stop;
ProgressBar pd;
MyTask as;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化數據
init();
}
private void init() {
button_start = (Button) this.findViewById(R.id.main_start);
button_stop = (Button) this.findViewById(R.id.main_stop);
pd = (ProgressBar) this.findViewById(R.id.main_progress);
button_start.setOnClickListener(this);
button_stop.setOnClickListener(this);
}
// 點擊方法
@Override
public void onClick(View arg0) {
// 點擊了開始
if (button_start.getId() == arg0.getId()) {
as = new MyTask();
// execute()中的參數可以不填,也可以傳一個或多個,
// 參數類型與doInBackground方法參數必須一致,因爲接受參數就是這個方法
as.execute();
// 點擊了關閉
} else if (button_stop.getId() == arg0.getId()) {
as.cancel(true);
}
}
// 寫一個類繼承AsyncTask
class MyTask extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
Log.i("qing", "onPreExecute");
}
@Override
protected String doInBackground(String... arg0) {
/*****************************************
* 這裏如果有循環,一定要寫在try裏面,同時一定要寫休眠sleep,
* 不然AsyncTask的cancel()方法執行時此AsyncTask無法停止,
* 因爲這整個AsyncTask停止時因爲它在休眠時Thread.sleep(),
* 點擊了執行cancel()會報異常,於是退出。不然就會一直循環,
* 雖然會執行cancel()方法,但這個AsyncTask不會停止。
*****************************************/
try {
for (int i = 0; i <= 100; i++) {
Thread.sleep(100);
publishProgress(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
pd.setProgress(values[0]);
Log.i("q", "onProgressUpdate");
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(), "進度結束", Toast.LENGTH_SHORT).show();
Log.i("q", "onPostExecute");
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
Log.i("q", "onCancelled");
}
}
}
調試截圖: