AsyncTask和AsyncQueryHandler之比较

定义AsyncTask子类

private class LoadContactsTask extends AsyncTask<Void, Void, List<TxrjContact>> {

    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected List<TxrjContact> doInBackground(Void... params) {
        List<TxrjContact> contacts = ContactDataManager.getContacts(mContext);
        return contacts;
    }
    /* (non-Javadoc)
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = ProgressDialog.show(mContext,
                null, "loading contacts, please wait a moment");
    }
    /* (non-Javadoc)
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(List<TxrjContact> result) {
        super.onPostExecute(result);
        mContacts = result;
        mListAdapter = new ContactListAdapter(mContext, mContacts, mQuickAlphaBar);
        mListView.setAdapter(mListAdapter);
        mProgressDialog.dismiss();
    }
}

在onCreate方法中使用AsyncTask

new LoadContactsTask().execute();

 

定义AsyncQueryHandler的子类

private class AsyncQueryContacts extends AsyncQueryHandler {

    public AsyncQueryContacts(ContentResolver cr) {
        super(cr);
    }
    @Override
    protected void onQueryComplete(int token, Object cookie, Cursor c) {
        List<TxrjContact> contacts = new ArrayList<TxrjContact>();
        HashMap<Integer, TxrjContact> contactMap = new HashMap<Integer, TxrjContact>();
        if(c != null) {
            while(c.moveToNext()) {
                int contactId = c.getInt(c.getColumnIndex(Data.CONTACT_ID));
                TxrjContact contact = null;
                if(contactMap.containsKey(contactId)) {
                    contact = contactMap.get(contactId);
                } else {
                    contact = new TxrjContact();
                    contact.setContactId(contactId);
                    contact.setName(c.getString(c.getColumnIndex(Phone.DISPLAY_NAME)));
                    contact.setSortKey(c.getString(c.getColumnIndex("sort_key")));
                    contactMap.put(contactId, contact);
                    contacts.add(contact);
                }
                TxrjPhone phone = new TxrjPhone();
                phone.setId(c.getInt(c.getColumnIndex(Data._ID)));
                phone.setRawContactId(c.getInt(c.getColumnIndex(Data.RAW_CONTACT_ID)));
                phone.setContactId(c.getInt(c.getColumnIndex(Data.CONTACT_ID)));
                phone.setNumber(c.getString(c.getColumnIndex(Phone.NUMBER)));
                phone.setType(c.getString(c.getColumnIndex(Phone.TYPE)));
                phone.setLabel(c.getString(c.getColumnIndex(Phone.LABEL)));
                contact.getPhoneList().add(phone);
            }
            c.close();
        }
        mContacts = contacts;
        mListAdapter = new ContactListAdapter(mContext, mContacts, mQuickAlphaBar);
        mListView.setAdapter(mListAdapter);
        mProgressDialog.dismiss();
        super.onQueryComplete(token, cookie, c);
    }
}

在onCreate方法中使用AsyncQueryHandler

private AsyncQueryContacts asyncQuery;

asyncQuery = new AsyncQueryContacts(getContentResolver());
mProgressDialog = ProgressDialog.show(mContext,
        null, "loading contacts, please wait a moment");
asyncQuery.startQuery(0, null, Data.CONTENT_URI,
    new String[] {Data._ID, Data.CONTACT_ID, Data.RAW_CONTACT_ID,
        Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, Phone.LABEL, "sort_key"},
    Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
    null, "sort_key COLLATE LOCALIZED asc");

posted on 2013-07-10 07:32  勤修  阅读(1115)  评论(0编辑  收藏  举报

导航