Android中的AsyncTask异步任务的简单实例

在 Android中的AsyncTask异步任务的简介 一文中。已经对 安卓 异步任务操作做了简单的介绍。这里,直接将上文中的异步任务做了一个实例。实现异步操作更新UI线程,相比开启子线程更新来说逻辑性更加合理 下面内容。可直接拷贝到项目中继而运行測试 activity_main.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"
tools:context=".MainActivity" >

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="58dp" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/progressBar"
android:layout_marginTop="18dp"
android:text="TextView" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Button" />

</RelativeLayout>
MainActivity.java
package com.example.asynctaskdemo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView tv;
private ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TestAsyncTask testAsyncTask = new TestAsyncTask();
testAsyncTask.execute(100);
}
});
tv = (TextView) findViewById(R.id.textView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

/*
* @TestAsyncTaskInteger 參数Integer 进度String 返回值 类型
*/
class TestAsyncTask extends AsyncTask<Integer, Integer, String> {

@Override
protected void onPreExecute() {
// 第一个运行方法
super.onPreExecute();
}

@Override
protected String doInBackground(Integer... params) {
// 第二个运行方法,onPreExecute()运行完后运行
for (int i = 0; i <= 100; i++) {
progressBar.setProgress(i);
publishProgress(i);
try {
Thread.sleep(params[0]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "运行完成";
}

@Override
protected void onProgressUpdate(Integer... progress) {
// 这个函数在doInBackground调用publishProgress时触发。尽管调用时仅仅有一个參数
// 可是这里取到的是一个数组,所以要用progesss[0]来取值
// 第n个參数就用progress[n]来取值
tv.setText(progress[0] + "%");
super.onProgressUpdate(progress);
}

@Override
protected void onPostExecute(String result) {
// doInBackground返回时触发。换句话说。就是doInBackground运行完后触发
// 这里的result就是上面doInBackground运行后的返回值,所以这里是"运行完成"
setTitle(result);
super.onPostExecute(result);
}

}
}
对于各參数及各方法的介绍使用。均已在凝视中解释,关于很多其它 安卓 方面的开法 可加Q群 104725817 讨论
原文地址:https://www.cnblogs.com/mthoutai/p/7050266.html