现在的位置: 首页 > 移动开发> 正文
AsyncQueryHandler
2011年06月30日 移动开发 暂无评论 ⁄ 被围观 3,546+

Class Overview

A helper class to help make handling asynchronous ContentResolver queries easier.

AsyncQueryHandler的目的就是为了将查询数据库的操作放到后台执行,当后台数据查询完了以后,再通知界面的更新,以此来提高前台页面的显示速度!

内部的实现机制其实就是Handler,我们自己平时做大数据量显示的时候也用到过,另起一个线程去执行任务,当后台计算完成的时候通过Handler发送Message来重绘界面,这个的实现原理类似,做了一层封装而已,方便开发人员的调用。

Public Methods
final        void cancelOperation(int token)

Attempts to cancel operation that has not already started.
void handleMessage(Message msg)

Subclasses must implement this to receive messages.
final void startDelete(int token, Object cookie, Uri uri, String selection, String[] selectionArgs)

This method begins an asynchronous delete.
final void startInsert(int token, Object cookie, Uri uri, ContentValues initialValues)

This method begins an asynchronous insert.
void startQuery(int token, Object cookie, Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy)

This method begins an asynchronous query.
final void startUpdate(int token, Object cookie, Uri uri, ContentValues values, String selection, String[] selectionArgs)

This method begins an asynchronous update.
Protected Methods
Handler createHandler(Looper looper)
void onDeleteComplete(int token, Object cookie, int result)

Called when an asynchronous delete is completed.
void onInsertComplete(int token, Object cookie, Uri uri)

Called when an asynchronous insert is completed.
void onQueryComplete(int token, Object cookie, Cursor cursor)

Called when an asynchronous query is completed.
void onUpdateComplete(int token, Object cookie, int result)

Called when an asynchronous update is completed.

 

其实这些方法都是成对来使用的,比如查询操作,我们通过调用 startDelete(int token, Object cookie, Uri uri, String selection, String[] selectionArgs)来执行,AsyncQueryHandler就会异步启动一个线程去做数据查询操作,当操作完成的时候就会回调onQueryComplete(int token, Object cookie, Cursor cursor)函数,我们在这个函数里面执行界面的刷新操作。

注:

1.  token参数是为了区分多次调用查询操作的每一次操作,在回调函数中来识别这是哪一个查询

2. cookie参数就是为了传递一个附加变量,在回调函数中可以使用

这两个参数在对应的查询函数和查询完成的回调函数中是一致的

代码示例:

我实现了一个小程序,用来显示手机上面的联系人信息,一开始列表显示假的数据,当后台联系人查询完成的时候通知界面刷新,显示真实的联系人数据,为了达到演示的效果,特别加入了一个延时语句,实际开发中没有必要!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.carey.com;
 
import java.util.ArrayList;
import java.util.List;
 
import android.app.ListActivity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
 
public class TestAsyncQueryHandlerActivity extends ListActivity {
private static final String TAG = "TestAsyncQueryHandlerActivity";
 
private Handler mHandler = new Handler();
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
Log.d(TAG, "before query:" + System.currentTimeMillis());
mHandler.postDelayed(new Runnable() {
public void run() {
QueryHandler qh = new QueryHandler(
TestAsyncQueryHandlerActivity.this.getContentResolver());
qh.startQuery(1, "111", Contacts.CONTENT_URI, null, null, null,
null);
}
}, 10000);
Log.d(TAG, "after query:" + System.currentTimeMillis());
 
// Cursor mCursor =
// this.getContentResolver().query(Contacts.CONTENT_URI,
// null, null, null, null);
// startManagingCursor(mCursor);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, getData()));
 
}
 
private List<String> getData() {
 
List<String> data = new ArrayList<String>();
data.add("测试数据1");
data.add("测试数据2");
data.add("测试数据3");
data.add("测试数据4");
 
return data;
}
 
/**
* Class used to run asynchronous queries to re-populate the notifications
* we care about.
*/
private class QueryHandler extends AsyncQueryHandler {
 
public QueryHandler(ContentResolver cr) {
super(cr);
}
 
@Override
public void startQuery(int token, Object cookie, Uri uri,
String[] projection, String selection, String[] selectionArgs,
String orderBy) {
Log.d(TAG, "<startQuery> token: " + token + ", cookie: " + cookie);
 
super.startQuery(token, cookie, uri, projection, selection,
selectionArgs, orderBy);
}
 
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
Log.d(TAG, "<onQueryComplete> token: " + token + ", cookie: "
+ cookie);
 
ListAdapter adapter = new SimpleCursorAdapter(
TestAsyncQueryHandlerActivity.this,
android.R.layout.two_line_list_item, cursor, new String[] {
Contacts.DISPLAY_NAME, Contacts.TIMES_CONTACTED },
new int[] { android.R.id.text1, android.R.id.text2 });
 
setListAdapter(adapter);
// TODO Auto-generated method stub
super.onQueryComplete(token, cookie, cursor);
}
 
}
}
package com.carey.com;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class TestAsyncQueryHandlerActivity extends ListActivity {
private static final String TAG = "TestAsyncQueryHandlerActivity";

private Handler mHandler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Log.d(TAG, "before query:" + System.currentTimeMillis());
mHandler.postDelayed(new Runnable() {
public void run() {
QueryHandler qh = new QueryHandler(
TestAsyncQueryHandlerActivity.this.getContentResolver());
qh.startQuery(1, "111", Contacts.CONTENT_URI, null, null, null,
null);
}
}, 10000);
Log.d(TAG, "after query:" + System.currentTimeMillis());

// Cursor mCursor =
// this.getContentResolver().query(Contacts.CONTENT_URI,
// null, null, null, null);
// startManagingCursor(mCursor);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, getData()));

}

private List<String> getData() {

List<String> data = new ArrayList<String>();
data.add("测试数据1");
data.add("测试数据2");
data.add("测试数据3");
data.add("测试数据4");

return data;
}

/**
* Class used to run asynchronous queries to re-populate the notifications
* we care about.
*/
private class QueryHandler extends AsyncQueryHandler {

public QueryHandler(ContentResolver cr) {
super(cr);
}

@Override
public void startQuery(int token, Object cookie, Uri uri,
String[] projection, String selection, String[] selectionArgs,
String orderBy) {
Log.d(TAG, "<startQuery> token: " + token + ", cookie: " + cookie);

super.startQuery(token, cookie, uri, projection, selection,
selectionArgs, orderBy);
}

@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
Log.d(TAG, "<onQueryComplete> token: " + token + ", cookie: "
+ cookie);

ListAdapter adapter = new SimpleCursorAdapter(
TestAsyncQueryHandlerActivity.this,
android.R.layout.two_line_list_item, cursor, new String[] {
Contacts.DISPLAY_NAME, Contacts.TIMES_CONTACTED },
new int[] { android.R.id.text1, android.R.id.text2 });

setListAdapter(adapter);
// TODO Auto-generated method stub
super.onQueryComplete(token, cookie, cursor);
}

}
}

官方链接: http://developer.android.com/reference/android/content/AsyncQueryHandler.html

给我留言

留言无头像?


×
腾讯微博