android之AsyncTask

android UI更新一般采用两种方式,第一种是利用Thread、Handler与Message结合,第二种是采用android系统中提供的AsyncTask。今天我们主要利用代码的方式说一下AsyncTask的使用,以下纯属自己在工作中或学习中的一些总结,请各位大牛们指教!

AsyncTask来自于Google官方的解释:

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as ExecutorThreadPoolExecutor and FutureTask.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called ParamsProgress and Result, and 4 steps, called onPreExecutedoInBackgroundonProgressUpdate and onPostExecute.

下面,我使用实例对AsyncTask对象的简单使用进行说明,该实例主要实现是利用android中的Activity、AsyncTask实现进度条的更新功能。代码如下:


package com.example.asynctaskui;


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.SeekBar;


public class Act_Main extends Activity {
private Button btnUpdate;
private SeekBar pbUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
this.btnUpdate = (Button) findViewById(R.id.btnUpdate);
this.pbUpdate = (SeekBar) findViewById(R.id.pbUpdate);
this.btnUpdate.setOnClickListener(clickListener);
}

OnClickListener clickListener = new OnClickListener() {


@Override
public void onClick(View view) {
UpdateProgressTask updateProgressTask = new UpdateProgressTask();
updateProgressTask.execute(100);
}

};


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.act_main, menu);
return true;
}


class UpdateProgressTask extends AsyncTask<Integer, Integer, Integer> {
@Override
protected Integer doInBackground(Integer... params) {
for (int i = 0; i < 100; i++) {
pbUpdate.setProgress(i);
publishProgress(i);
try {
Thread.sleep(params[0]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return 0;
}


@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);

}


@Override
protected void onPreExecute() { 
super.onPreExecute();
}


@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);

}

}
}

在使用AsyncTask时,涉及到几个方法以及其执行顺序如下:

首先执行onPreExecute()函数,一般用于在执行AsyncTask任务时,对UI作的标记;其次执行doInBackground()函数,该方法用于执行比较耗时的操作,同时该方法接收输入参数返回计算结构,在该函数中可以利用publishProgress()函数,更新进度。onPregressUpdate()函数是在调用publishProgress()函数执行,直接将进度值更新到界面上。onPostExecute函数式在当后台操作结束后执行,直接将返回结果更新到界面上。好了,就这么多了,如果大家有什么疑问我们彼此多交流吧!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值