AsyncQueryHandler

Class Overview

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

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

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

 
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.
 
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参数就是为了传递一个附加变量,在回调函数中可以使用

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

代码示例:

参见: http://carey.sinaapp.com/?p=89

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值