編輯:關於android開發
Class Overview
A helper class to help make handling asynchronous queries easier.
AsyncQueryHandler的目的就是為了將查詢數據庫的操作放到後台執行,當後台數據查詢完了以後,再通知界面的更新,以此來提高前台頁面的顯示速度!
內部的實現機制其實就是Handler,我們自己平時做大數據量顯示的時候也用到過,另起一個線程去執行任務,當後台計算完成的時候通過Handler發送Message來重繪界面,這個的實現原理類似,做了一層封裝而已,方便開發人員的調用。
Attempts to cancel operation that has not already started.
void ( msg)
Subclasses must implement this to receive messages.
final void (int token, cookie, uri, selection, selectionArgs)
This method begins an asynchronous delete.
final void (int token, cookie, uri, initialValues)
This method begins an asynchronous insert.
void (int token, cookie, uri, projection, selection, selectionArgs, orderBy)
This method begins an asynchronous query.
final void (int token, cookie, uri, values, selection, selectionArgs)
This method begins an asynchronous update.
Called when an asynchronous delete is completed.
void (int token, cookie, uri)
Called when an asynchronous insert is completed.
void (int token, cookie, cursor)
Called when an asynchronous query is completed.
void (int token, cookie, int result)
Called when an asynchronous update is completed.
其實這些方法都是成對來使用的,比如查詢操作,我們通過調用 (int token, cookie, uri, selection, selectionArgs)來執行,AsyncQueryHandler就會異步啟動一個線程去做數據查詢操作,當操作完成的時候就會回調(int token, cookie, 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);
- }
- }
- }
官方鏈接:
轉自:
Linux內核系列—操作系統開發之進入32位保護模式,linux保護模式源碼如下: ; ========================================
Android網絡編程的Socket通信總結 創建服務器端的步驟: 1,指定端口實例化一個ServerSocket 2,調用ServerSocket的accept方法等待
對於任何軟件來說,美觀的界面都是用戶體驗的重要組成部分,它能提高整個軟件的品質
【Android】不彈root請求框檢測手機是否root,androidroot由於項目需要root安裝軟件,並且希望在合適的時候引導用戶去開啟root安裝,故需要檢測手