<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");
}
}
}
调试截图: