首先需要設置MyAsyncTask類。
public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Log.d("abc", "doInBackground");
publishProgress();
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("abc", "onPreExecute");
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Log.d("abc", "onPostExecute");
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
Log.d("abc", "onProgressUpdate");
}
}
ProgressBar調用流程:
首先創建MyAsyncTask,聲明成員變量myAsyncTask,在onCreate()方法中實例化並啓動異步處理;
接着,在doInBackground方法中寫一個for循環用來模擬進度更新;
最後,在onProgressUpdate()方法中獲取到所傳遞進來的更新值。
public class ProcessBar extends Activity {
private ProgressBar progressBar;
private MyAsyncTask myAsyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.processbar);
progressBar = (ProgressBar) findViewById(R.id.pg);
myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
// 讓ProgressBar的生命週期等於Activity的生命週期
@Override
protected void onPause() {
super.onPause();
if (myAsyncTask != null
&& myAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
// cancel()方法只是將對應的AsyncTask標記爲cancel狀態,並不是真正的線程執行
myAsyncTask.cancel(true);
}
}
class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
// 模擬進度更新
for (int i = 1; i < 101; i++) {
if (myAsyncTask.isCancelled()) {
break;
}
publishProgress(i);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// 獲取進度更新值
if (myAsyncTask.isCancelled()) {
return;
}
progressBar.setProgress(values[0]);// 從values數組中取出第0位作爲我們的當前進度值,由於只傳遞一個值,所以只需要獲取0就行了
}
}
}
設置xml文件。
<RelativeLayout 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"
android:padding="16dp"
tools:context=".ProcessBar" >
<ProgressBar
android:id="@+id/pg"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<com.example.myasynctask.MyData
android:id="@+id/myData1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/pg"
android:layout_centerHorizontal="true"
android:layout_marginBottom="39dp" />
</RelativeLayout>
在MainActivity中開始異步操作。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyAsyncTask task = new MyAsyncTask();
task.execute();
}
public void loadProgressBar(View view) {
startActivity(new Intent(this, ProcessBar.class));
}
}
<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"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadProgressBar"
android:text="ProgressBar Test" />
</LinearLayout>
最後,在清單文件中註冊網絡權限。
<uses-permission android:name="android.permission.INTERNET" />