<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start"
/>
<ProgressBar
android:id="@+id/bar"
android:layout_width="200dp"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:visibility="gone"
/>
<TextView
android:id="@+id/percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="0%"
android:visibility="gone"
android:layout_marginLeft="90dp"
android:layout_marginTop="-18dp"
android:textColor="#000"
/>
</LinearLayout>
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
publicclass AsyncTaskDemoActivity extends Activity {
private ProgressBar bar = null;
private Button start = null;
private TextView percent = null;
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button)findViewById(R.id.start); //Start按钮
start.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View arg0) {
// TODO Auto-generated method stub
SubAsyncTask sat = new SubAsyncTask(); //创建AsyncTask子类的实例
sat.execute(); //启动线程
}
});
}
publicclass SubAsyncTask extends AsyncTask<Object, Integer, String>{
@Override
protectedvoid onPreExecute() {
// TODO Auto-generated method stub
System.out.println("Thread in onPreExecute---->"+Thread.currentThread().getName());
//在预处理方法中定义进度条和百分比文本
bar = (ProgressBar)findViewById(R.id.bar);
bar.setVisibility(View.VISIBLE);
percent = (TextView)findViewById(R.id.percent);
percent.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected String doInBackground(Object... arg0) {
// TODO Auto-generated method stub
//每50ms执行一次publishProgress
for (int j = 0; j <=100; j+=10) {
System.out.println("Thread in doInBackground---->"+Thread.currentThread().getName());
publishProgress(j);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
returnnull;
}
@Override
protectedvoid onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
//每执行一次publishProgress就需要执行一次这个函数,在这个函数里变化进度条信息
System.out.println("Thread in onProgressUpdate---->"+Thread.currentThread().getName());
bar.setProgress(values[0]);
percent.setText(values[0]+"%");
}
@Override
protectedvoid onPostExecute(String result) {
// TODO Auto-generated method stub
//doInBackground方法执行完后,执行这个函数,用来提交结果
bar.setVisibility(View.GONE);
percent.setVisibility(View.GONE);
System.out.println("Thread in onPostExecute---->"+Thread.currentThread().getName());
super.onPostExecute(result);
}
}
}
转载于:https://blog.51cto.com/theron/658455