AsyncQueryHandler
extends Handler
↳ | ||
| ↳ | android.content.AsyncQueryHandler |
AsyncQueryHandler:异步的查询操作帮助类,使用它可以更简单的对ContentResolver进行操作。
构造方法 | |||||||||||
公共方法 | |||||||||||
final void | cancelOperation(int token) 动作还没有开始前,取消操作 | ||||||||||
void | handleMessage(Message msg) 子类必须实现这个来接收消息。 | ||||||||||
final void | startDelete(int token, Object cookie, Uri uri, String selection, String[] selectionArgs) 一个异步删除的方法。 | ||||||||||
final void | startInsert(int token, Object cookie, Uri uri, ContentValues initialValues) 执行一个异步的插入. | ||||||||||
void | startQuery(int token, Object cookie, Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy) 执行一个异步的查询 | ||||||||||
final void | startUpdate(int token, Object cookie, Uri uri, ContentValues values, String selection, String[] selectionArgs) 执行一个异步的更新 |
受保护方法 | |||||||||||
createHandler(Looper looper) | |||||||||||
void | onDeleteComplete(int token, Object cookie, int result) 异步删除完成时调用。 | ||||||||||
void | onInsertComplete(int token, Object cookie, Uri uri) 异步插入完成时调用。 | ||||||||||
void | onQueryComplete(int token, Object cookie, Cursor cursor) 异步查询完成时调用。 | ||||||||||
void | onUpdateComplete(int token, Object cookie, int result) 异步更新完成时调用。 |
public final void startDelete (int token, Object cookie, Uri uri, String selection, String[] selectionArgs)
参数
token | 操作的辨识标记 |
cookie | 一个对象被传递到 onDeleteComplete(int, Object, int)中去 |
uri | 需要执行删除操作的URI |
selection | 条件参数 |
public final void startInsert (int token, Object cookie, Uri uri, ContentValues initialValues)
参数
token | 在 onInsertComplete(int, Object, Uri)中需要的辨识Token |
cookie | 传入一个对象到onInsertComplete(int, Object, Uri)中去 |
uri | 需要执行插入操作的URI |
initialValues | 需要插入的值 |
public void startQuery (int token, Object cookie, Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy)
参数
token | 在onQueryComplete中用作操作标识 |
cookie | |
uri | The URI, using the content:// scheme, for the content to retrieve. |
projection | 需要查询的列 |
selection | 查询的条件语句 |
selectionArgs | 查询的条件参数 |
orderBy | 结果排序操作与 SQL 中的ORDER BY 一致
|
public final void startUpdate (int token, Object cookie, Uri uri, ContentValues values, String selection, String[] selectionArgs)
参数
token | 传入 onUpdateComplete(int, Object, int) 中的辨识符 |
cookie | |
uri | 执行更新操作的URI |
values | 需要更新的值 |
一个简单的 查询demo
private static final int QUERY_CONTACT = 1;
private static final int QUERY_GROUP = 2;
private Uri contact_uri = Phone.CONTENT_URI; // 联系人的Uri
String[] projection =
{
Phone._ID,
Phone.DISPLAY_NAME,
Phone.DATA1,
Phone.SORT_KEY_PRIMARY,
Phone.CONTACT_ID,
Phone.PHOTO_ID,
Phone.LOOKUP_KEY
}; // 查询的列
/**
* 数据库异步查询类AsyncQueryHandler
*
* @author administrator
*/
private class ContactQueryHandler extends AsyncQueryHandler
{
public ContactQueryHandler(ContentResolver cr)
{
super(cr);
}
/**
* 查询结束的回调函数
*/
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor)
{
switch (token)
{
case QUERY_CONTACT:
getContact(cursor);
break;
case QUERY_GROUP:
break;
default:
break;
}
}
}
private void getContact(Cursor cursor)
{
List<ContactBean> contacts = new ArrayList<ContactBean>();
if (cursor != null && cursor.getCount() > 0)
{
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++)
{
ContactBean contactBean;
cursor.moveToPosition(i);
String name = cursor.getString(1);
String number = cursor.getString(2);
String sortKey = cursor.getString(3);
int contactId = cursor.getInt(4);
Long photoId = cursor.getLong(5);
String lookUpKey = cursor.getString(6);
if (contactSparse.get(contactId) != null)
{
contactBean = contactSparse.get(contactId);
}
else
{
contactBean = new ContactBean();
contactBean.displayName = name;
contactBean.phoneNum = number;
contactBean.sortKey = sortKey;
contactBean.contactId = contactId;
contactBean.photoId = photoId;
contactBean.lookUpKey = lookUpKey;
contactSparse.put(contactId, contactBean);
}
contacts.add(contactBean);
}
if (m_getContactListener != null)
m_getContactListener.onGetContact(contacts);
}
执行,查询query就能够得到了
asyncQuery.startQuery(QUERY_CONTACT, null, contact_uri, projection, null, null,
"sort_key COLLATE LOCALIZED asc"); // 按照sort_key升序查询