編輯:Android開發實例
Class Overview
A helper class to help make handling asynchronous ContentResolver
queries easier.
AsyncQueryHandler的目的就是為了將查詢數據庫的操作放到後台執行,當後台數據查詢完了以後,再通知界面的更新,以此來提高前台頁面的顯示速度!
內部的實現機制其實就是Handler,我們自己平時做大數據量顯示的時候也用到過,另起一個線程去執行任務,當後台計算完成的時候通過Handler發送Message來重繪界面,這個的實現原理類似,做了一層封裝而已,方便開發人員的調用。
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.
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參數就是為了傳遞一個附加變量,在回調函數中可以使用
這兩個參數在對應的查詢函數和查詢完成的回調函數中是一致的
代碼示例:
我實現了一個小程序,用來顯示手機上面的聯系人信息,一開始列表顯示假的數據,當後台聯系人查詢完成的時候通知界面刷新,顯示真實的聯系人數據,為了達到演示的效果,特別加入了一個延時語句,實際開發中沒有必要!
- 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
轉自:http://blog.zhourunsheng.com/2011/06/asyncqueryhandler/
引言 應用程序組件有一個生命周期——一開始Android實例化他們響應意圖,直到結束實例被銷毀。在這期間,他們有時候處於激活狀態,有時候處於非激活狀 態;對於活
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
本文實例講述了Android編程實現ImageView圖片拋物線動畫效果的方法。分享給大家供大家參考,具體如下: 想實現拋物線動畫,必須知道拋物線的方程,這時候數
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放