Android之 AsyncTask
来源:互联网 发布:电脑桌面没有网络图标 编辑:程序博客网 时间:2024/05/13 05:17
转自文章http://yixiong89921.blog.163.com/blog/static/132537788201227104947389/
由于文笔有限,所以建议先看一下这篇文章
http://www.cnblogs.com/dawei/archive/2011/04/18/2019903.html
这里再简单说明一下,AsyncTask的作用其实相当于一个后台线程,它可用在它内部更新UI界面,而不必在UI线程中使用Handler,这个东东呢主要用来处理一些比较耗时的逻辑比较简单的事务处理,如果是复杂的处理,建议还是使用线程后台执行。
AsyncTask是抽象类.AsyncTask定义了三种泛型类型 Params,Progress和Result。
Params 启动任务执行的输入参数,比如HTTP请求的URL。
Progress 后台任务执行的百分比。
Result 后台执行任务最终返回的结果,比如String。
也就是说,我们在定义自己的AsyncTask的时候,这3个参数的类型可用根据自己的需要来进行定义。这三个参数跟AsyncTask的内部实现有很大关系,在AsyncTask的内部实现过程中,我们需要重写几个函数,这几个函数是系统调用的,我们不能自己调用。这几个函数分别是:
1、 onPreExecute(), 该方法将在执行实际的后台操作前被UI thread调用。可以在该方法中做一些准备工作,如在界面上显示一个进度条。
2、 doInBackground(Params...), 该方法是抽象方法,子类必须实现。
将在onPreExecute 方法执行后马上执行,该方法运行在后台线程中。这里将主要负责执行那些很耗时的后台计算工作。可以调用AsyncTask内部的publishProgress方法来更新实时的任务进度,publishProgress会调用我们定义的onProgressUpdate(Progress...)方法。
3、 onProgressUpdate(Progress...),在publishProgress方法被调用后,UI thread将调用这个方法从而在界面上展示任务的进展情况,例如通过一个进度条进行展示。 所以我们的更新界面的代码在这个函数中实现。它的传入参数就是定义AsyncTask类型时指定的第二个参数。
由publishProgress方法传入
4、 4、 onPostExecute(Result), 在doInBackground 执行完成后,onPostExecute 方法将被UI thread调用,后台的计算结果将通过该方法传递到UI thread. 它的参数就是定义AsyncTask类型时指定的第三个参数。该参数的值由doInBackground方法返回
代码说明:
package Test.Test;import java.io.IOException;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import org.xmlpull.v1.XmlPullParserException;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class AsyncTaskTestActivity extends Activity { /** Called when the activity is first created. */ EditText editText; Button btnButton; TextView tView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText=(EditText)findViewById(R.id.editText1); editText.setText("hhhhhh"); tView=(TextView)findViewById(R.id.textView1); btnButton=(Button)findViewById(R.id.button1); btnButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub MyAsyncTask myAsyncTask=new MyAsyncTask();//该对象必须在UI线程中创建 myAsyncTask.execute(editText.getText().toString());//execute方法必须在UI线程中调用,传入参数是定义AsyncTask时的第一个参数 } }); } private class MyAsyncTask extends AsyncTask<String, String, String> { @Override protected void onCancelled() { // TODO Auto-generated method stub super.onCancelled(); } //result的类型是定义AsyncTask时的第三个参数类型,其值由doInBackground方法返回 @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); tView.setText("结果为"+result); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); editText.setText("异步准备执行"); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } ////values的类型是定义AsyncTask时的第2个参数类型,其值由doInBackground执行过程中调用publishProgress方法传递 @Override protected void onProgressUpdate(String... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); tView.setText(values[0]); } //长时运行的后台处理,参数是定义AsyncTask时的第1个参数,我们调用execute方法时传入 @Override protected String doInBackground(String... params) { int i=0; while(i<20) { publishProgress((i++)+""); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return i+""; } }}
- android 线程之AsyncTask
- Android之AsyncTask介绍
- android多线程之AsyncTask
- android之AsyncTask
- Android之AsyncTask(一)
- Android之AsyncTask(二)
- android之AsyncTask
- Android--多线程之AsyncTask
- Android学习之 AsyncTask
- Android学习之 AsyncTask
- Android之AsyncTask面试
- Android之 AsyncTask
- android多线程之AsyncTask
- android之AsyncTask
- Android API 之 AsyncTask
- Android之AsyncTask类
- android多线程之AsyncTask
- Android 之AsyncTask 学习
- 远程连接oracle数据库
- 程序中控制是否接收推送通知
- DB2常用命令总结
- 做了一些心理学的测试,分析下个人
- 初级网络编程
- Android之 AsyncTask
- PCL读取与保存点云文件
- 解决HttpClient交付json格式请求的中文支持
- Android Voip开源客户端比较
- android 获取控件在屏幕里的坐标
- 关于mysql存储过程的definer的问题
- Lua基础教程
- ExtJS4学习笔记七--事件绑定相关
- Mac OS X 下配置 GTK 开发环境(Homebrew)