Android异步任务的简单呢操作,以及运行过程。
1.这是个主界面,里面包含一个TextView和ProgressBar和Button.主要看Button 监听中的内容.
MainActivity
package David.async;
import android.app.Activity;
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;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private TextView bt;
private ProgressBar bar;
private Button qd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(TextView)findViewById(R.id.bt);
bar=(ProgressBar)findViewById(R.id.progressBar1);
qd=(Button)findViewById(R.id.qd);
qd.setOnClickListener(new qdListener());
}
private class qdListener implements OnClickListener{
@Override
public void onClick(View v) {
//调用异步任务类,传递空间和参数
//使用execute来启动异步任务
OneAsyncTask asyncTask=new OneAsyncTask(bt, bar);
asyncTask.execute(1000);
}
}
}
2.模拟网络延迟创建的线程类:
XCxiumian
package David.async;
public class XCxiumian {
//此线程是模拟网络操作产生的延迟
public void xiumian(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.异步任务操作类:
OneAsyncTask
package David.async;
import java.security.PublicKey;
import android.R.integer;
import android.os.AsyncTask;
import android.widget.ProgressBar;
import android.widget.TextView;
//进入此类时,调用的是onPreExecute();
//然后调用doInBackground()
//然后通过publishProgress()进入到onProgressUpdate()
//最后是onPostExecute();
public class OneAsyncTask extends AsyncTask<Integer, Integer, String>{
private TextView bt;
private ProgressBar bar;
public OneAsyncTask(TextView textview,ProgressBar bar){
this.bt=textview;
this.bar=bar;
}
//在doInBackground方法中不能直接对控件进行操作,只能通过publishProgress()来传递参数到onProgressUpdate()中才能对控件操作
@Override
protected String doInBackground(Integer... params) {
int i=0;
for(i=10;i<=100;i+=10)
{
XCxiumian cxiumian=new XCxiumian();
cxiumian.xiumian();
//使用此方法传递参数到onProgressUpdate中。
publishProgress(i);
}
return i+params[0].intValue()+"";
}
//当异步任务结束的时候调用此方法
@Override
protected void onPostExecute(String result) {
bt.setText("异步任务结束");
super.onPostExecute(result);
}
//启动异步任务时候最先调用此方法
@Override
protected void onPreExecute() {
bt.setText("异步任务开始");
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
int value=values[0];
bar.setProgress(value);
super.onProgressUpdate(values);
}
}
4.布局文件:
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/bt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressBar1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
</ProgressBar>
<Button
android:id="@+id/qd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动一个异步任务"
/>
</LinearLayout>


浙公网安备 33010602011771号