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