Android中AsyncTask类与AsyncQueryHandler类的用法

来源:互联网 发布:财政部会计报表软件 编辑:程序博客网 时间:2024/06/05 05:23
AsyncTask类

一.AsyncTask 是Android 1.5提供了一个工具类,它使创建需要与用户界面交互的长时间
运行的任务变得更简单。相对来说AsyncTask更轻量级一些,适用于简单的异步处理,
不需要借助线程和Handler即可实现。

二.AsyncTask是抽象类.AsyncTask定义了三种泛型类型 Params,Progress和Result。
Params 启动任务执行的输入参数,比如HTTP请求的URL。
Progress 后台任务执行的百分比。
Result 后台执行任务最终返回的结果,比如String。

三.AsyncTask的执行分为四个步骤,每一步都对应一个回调方法,这些方法不应该由应用
程序调用,开发者需要做的就是实现这些方法。
  1) 子类化AsyncTask
  2) 实现AsyncTask中定义的下面一个或几个方法
  ①onPreExecute(), 该方法将在执行实际的后台操作前被UI thread调用。可以在该
方法中做一些准备工作,如在界面上显示一个进度条。
   ②doInBackground(Params...), 将在onPreExecute 方法执行后马上执行,该方法运行
在后台线程中。这里将主要负责执行那些很耗时的后台计算工作。可以调用 publishProgress方法来更新实时的任务进度。该方法是抽象方法,子类必须实现。
   ③onProgressUpdate(Progress...),在publishProgress方法被调用后,UI thread将调用这
个方法从而在界面上展示任务的进展情况,例如通过一个进度条进行展示。
   ④onPostExecute(Result), 在doInBackground 执行完成后,onPostExecute 方法将被
UI thread调用,后台的计算结果将通过该方法传递到UI thread.

四.为了正确的使用AsyncTask类,以下是几条必须遵守的准则:
  1) Task的实例必须在UI thread中创建
  2) execute方法必须在UI thread中调用
3) 不要手动的调用onPreExecute(), onPostExecute(Result),doInBackground(Params...),
onProgressUpdate(Progress...)这几个方法
  4) 该task只能被执行一次,否则多次调用时将会出现异常
doInBackground方法和onPostExecute的参数必须对应,这两个参数在AsyncTask

声明的泛型参数列表中指定,第一个为doInBackground接受的参数,第二个为显示进度的参数,第三个为doInBackground返回和onPostExecute传入的参数。



[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package test.list;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.InputStream;  
  4. import java.util.ArrayList;  
  5.    
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.client.HttpClient;  
  9. import org.apache.http.client.methods.HttpGet;  
  10. import org.apache.http.impl.client.DefaultHttpClient;  
  11.    
  12. import android.app.Activity;  
  13. import android.app.ProgressDialog;  
  14. import android.content.Context;  
  15. import android.content.DialogInterface;  
  16. import android.os.AsyncTask;  
  17. import android.os.Bundle;  
  18. import android.os.Handler;  
  19. import android.os.Message;  
  20. import android.view.View;  
  21. import android.widget.Button;  
  22. import android.widget.EditText;  
  23. import android.widget.TextView;  
  24.    
  25. public class NetworkActivity extends Activity{  
  26.     private TextView message;  
  27.     private Button open;  
  28.     private EditText url;  
  29.    
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.        super.onCreate(savedInstanceState);  
  33.        setContentView(R.layout.network);  
  34.        message= (TextView) findViewById(R.id.message);  
  35.        url= (EditText) findViewById(R.id.url);  
  36.        open= (Button) findViewById(R.id.open);  
  37.        open.setOnClickListener(new View.OnClickListener() {  
  38.            public void onClick(View arg0) {  
  39.               connect();  
  40.            }  
  41.        });  
  42.    
  43.     }  
  44.    
  45.     private void connect() {  
  46.         PageTask task = new PageTask(this);  
  47.         task.execute(url.getText().toString());  
  48.     }  
  49.    
  50.    
  51.     class PageTask extends AsyncTask {  
  52.         // 可变长的输入参数,与AsyncTask.exucute()对应  
  53.         ProgressDialog pdialog;  
  54.         public PageTask(Context context){  
  55.             pdialog = new ProgressDialog(context, 0);    
  56.             pdialog.setButton("cancel"new DialogInterface.OnClickListener() {  
  57.              public void onClick(DialogInterface dialog, int i) {  
  58.               dialog.cancel();  
  59.              }  
  60.             });  
  61.             pdialog.setOnCancelListener(new DialogInterface.OnCancelListener() {  
  62.              public void onCancel(DialogInterface dialog) {  
  63.               finish();  
  64.              }  
  65.             });  
  66.             pdialog.setCancelable(true);  
  67.             pdialog.setMax(100);  
  68.             pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  69.             pdialog.show();  
  70.    
  71.    
  72.         }  
  73.         @Override  
  74.         protected String doInBackground(String... params) {  
  75.    
  76.             try{  
  77.    
  78.                HttpClient client = new DefaultHttpClient();  
  79.                // params[0]代表连接的url  
  80.                HttpGet get = new HttpGet(params[0]);  
  81.                HttpResponse response = client.execute(get);  
  82.                HttpEntity entity = response.getEntity();  
  83.                long length = entity.getContentLength();  
  84.                InputStream is = entity.getContent();  
  85.                String s = null;  
  86.                if(is != null) {  
  87.                    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  88.    
  89.                    byte[] buf = new byte[128];  
  90.    
  91.                    int ch = -1;  
  92.    
  93.                    int count = 0;  
  94.    
  95.                    while((ch = is.read(buf)) != -1) {  
  96.    
  97.                       baos.write(buf, 0, ch);  
  98.    
  99.                       count += ch;  
  100.    
  101.                       if(length > 0) {  
  102.                           // 如果知道响应的长度,调用publishProgress()更新进度  
  103.                           publishProgress((int) ((count / (float) length) * 100));  
  104.                       }  
  105.    
  106.                       // 让线程休眠100ms  
  107.                       Thread.sleep(100);  
  108.                    }  
  109.                    s = new String(baos.toByteArray());              }  
  110.                // 返回结果  
  111.                return s;  
  112.             } catch(Exception e) {  
  113.                e.printStackTrace();  
  114.    
  115.             }  
  116.    
  117.             return null;  
  118.    
  119.         }  
  120.    
  121.         @Override  
  122.         protected void onCancelled() {  
  123.             super.onCancelled();  
  124.         }  
  125.    
  126.         @Override  
  127.         protected void onPostExecute(String result) {  
  128.             // 返回HTML页面的内容  
  129.             message.setText(result);  
  130.             pdialog.dismiss();  
  131.         }  
  132.    
  133.         @Override  
  134.         protected void onPreExecute() {  
  135.             // 任务启动,可以在这里显示一个对话框,这里简单处理  
  136.             message.setText(R.string.task_started);  
  137.         }  
  138.    
  139.         @Override  
  140.         protected void onProgressUpdate(Integer... values) {  
  141.             // 更新进度  
  142.               System.out.println(""+values[0]);  
  143.               message.setText(""+values[0]);  
  144.               pdialog.setProgress(values[0]);  
  145.         }  
  146.    
  147.      }  
  148.    
  149. }  



AsyncQueryHandler类
一.AsyncQueryHandler:异步的查询操作帮助类,它可以处理增删改查。

二.AsyncQueryHandler的作用:它提供:
startInsert,startDelete,startUpdate,startQuery这四个操作,并提供相对应的onInsertComplete,onDeleteComplete,onUpdateComplete,onQuerytComplete四个方法,以供操作完数据库后进行其它的操作,这四个 onXXXComplete方法都是空实现,以便我们只需要去实现我们关注的操作。

三.AsyncQueryHandler的使用方法:
1)需要继承AsyncQueryHandler类,并提供onInsertComplete方法的实现(可以实现任何一个或多个,当然你也可以一个也不实现,如果你不关注操作数据库的結果),在你的实现中做一些对数据库操作完成的处理。
2)使用时直接调用startQuery(或者其他四个中的任何一个)方法即可。传入的通用参数如下:
①token,一个令牌,需要跟onQueryComplete方法传入的一致。(当然你也可以不一
致,同样在数据库的操作结束后会调用对应的onQueryComplete方法 )。
②cookie,你想传给onQueryComplete方法使用的一个对象。(没有的话传递null即可)。
③Uri uri(进行查询的通用资源标志符)。
④projection 查询的列
⑤selection 限制条件

⑥orderBy 排序条件

ContactsAsyncQueryHandler asyncQuery = new ContactsAsyncQueryHandler(context,listView,list,alphaMap);        asyncQuery.startQuery(0,            null,            Phone.CONTENT_URI,            new String[] {Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER,Phone.SORT_KEY_PRIMARY},            Phone.TYPE + "='" + Phone.TYPE_MOBILE + "'",            null,            Phone.SORT_KEY_PRIMARY + " COLLATE LOCALIZED asc");

public class ContactsAsyncQueryHandler extends AsyncQueryHandler{    private Context context;        private ListView listView;        private List<ContactsEntity> list;        private HashMap<String, Integer> alphaMap;        public ContactsAsyncQueryHandler(Context context, ListView listView, List<ContactsEntity> list,        HashMap<String, Integer> alphaMap)    {        super(context.getContentResolver());        this.context = context;        this.listView = listView;        this.list = list;        this.alphaMap = alphaMap;    }        @Override    protected void onQueryComplete(int token, Object cookie, Cursor cursor)    {        cursor.moveToFirst();        while (cursor.moveToNext())        {            ContactsEntity entity = new ContactsEntity();            entity.setName(cursor.getString(1));            entity.setNumber(cursor.getString(2));            entity.setSortKey(cursor.getString(3));            entity.setHeadIamgeUri(getContactImageUri(cursor.getString(0)));            list.add(entity);        }        if (list.size() > 0)        {            setAdapter();            initAlphaMap();        }    }}



0 0
原创粉丝点击