---
title: "在Android中根据联系人查询电话号码"
author: "cqfly"
date: "2011-04-03T03:27:48Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2011-04-14T16:41:03Z"
id: 985564
url: "https://cqfly.iteye.com/blog/985564"
---

详见在Android 2.2源码 packages/apps/Phone/tests/src/com/android/phone/NotificationMgr.java 模块中异步查询机制：

 

 

 

(1)开始查询

 

  // instantiate query handler

        mQueryHandler = new QueryHandler(mContext.getContentResolver());

 

        // setup query spec, look for all Missed calls that are new.

        StringBuilder where = new StringBuilder("type=");

        where.append(Calls.MISSED\_TYPE);

        where.append(" AND new=1");

 

        // start the query

        mQueryHandler.startQuery(CALL\_LOG\_TOKEN, null, Calls.CONTENT\_URI,  CALL\_LOG\_PROJECTION,

                where.toString(), null, Calls.DEFAULT\_SORT\_ORDER);

(2)异步查询类

 

/\**  
     * Class used to run asynchronous queries to re-populate  
     * the notifications we care about.  
     \*/  
    private class QueryHandler extends AsyncQueryHandler {

        /\**  
         * Used to store relevant fields for the Missed Call  
         * notifications.  
         \*/  
        private class NotificationInfo {   **//Modle**  
            public String name;  
            public String number;  
            public String label;  
            public long date;  
            public int subscription;  
        }

        public QueryHandler(ContentResolver cr) {  
            super(cr);  
        }

        /\**  
         * Handles the query results.  There are really 2 steps to this,  
         * similar to what happens in RecentCallsListActivity.  
         *  1. Find the list of missed calls  
         *  2. For each call, run a query to retrieve the caller's name.  
         \*/  
        @Override  
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {  
            // TODO: it would be faster to use a join here, but for the purposes  
            // of this small record set, it should be ok.

            // Note that CursorJoiner is not useable here because the number  
            // comparisons are not strictly equals; the comparisons happen in  
            // the SQL function PHONE\_NUMBERS\_EQUAL, which is not available for  
            // the CursorJoiner.

            // Executing our own query is also feasible (with a join), but that  
            // will require some work (possibly destabilizing) in Contacts  
            // Provider.

            // At this point, we will execute subqueries on each row just as  
            // RecentCallsListActivity.java does.  
            switch (token) {  
                case CALL\_LOG\_TOKEN:  
                    if (DBG) log("call log query complete.");

                    // initial call to retrieve the call list.  
                    if (cursor != null) {  
                        while (cursor.moveToNext()) {  
                            // for each call in the call log list, create  
                            // the notification object and query contacts  
                            NotificationInfo n = getNotificationInfo (cursor);

                            if (DBG) log("query contacts for number: " + n.number);  //从号码中查询联系人

                            mQueryHandler.startQuery(CONTACT\_TOKEN, n,  
                                    Uri.withAppendedPath(PhoneLookup.CONTENT\_FILTER\_URI, n.number),  
                                    PHONES\_PROJECTION, null, null, PhoneLookup.NUMBER);  
                        }

                        if (DBG) log("closing call log cursor.");  
                        cursor.close();  
                    }  
                    break;  
                case CONTACT\_TOKEN:  
                    if (DBG) log("contact query complete.");

                    // subqueries to get the caller name.  
                    if ((cursor != null) && (cookie != null)){  
                        NotificationInfo n = (NotificationInfo) cookie;

                        if (cursor.moveToFirst()) {  
                            // we have contacts data, get the name.  
                            if (DBG) log("contact :" + n.name + " found for phone: " + n.number);  
                            n.name = cursor.getString(  
                                    cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY\_NAME));  
                        }

                        // send the notification  
                        if (DBG) log("sending notification.");  
                        if (TelephonyManager.isDsdsEnabled()) {  
                            notifyMissedCall(n.name, n.number, n.label, n.date, n.subscription);  
                        } else {  
                            notifyMissedCall(n.name, n.number, n.label, n.date);  
                        }

                        if (DBG) log("closing contact cursor.");  
                        cursor.close();  
                    }  
                    break;  
                default:  
            }  
        }

 

 

/\**

         * Factory method to generate a NotificationInfo object given a

         * cursor from the call log table.

         \*/

        private final NotificationInfo getNotificationInfo(Cursor cursor) {

            NotificationInfo n = new NotificationInfo();

            n.name = null;

            n.number = cursor.getString(cursor.getColumnIndexOrThrow(Calls.NUMBER));

            n.label = cursor.getString(cursor.getColumnIndexOrThrow(Calls.TYPE));

            n.date = cursor.getLong(cursor.getColumnIndexOrThrow(Calls.DATE));

 

            // make sure we update the number depending upon saved values in

            // CallLog.addCall().  If either special values for unknown or

            // private number are detected, we need to hand off the message

            // to the missed call notification.

            if ( (n.number.equals(CallerInfo.UNKNOWN\_NUMBER)) ||

                 (n.number.equals(CallerInfo.PRIVATE\_NUMBER)) ||

                 (n.number.equals(CallerInfo.PAYPHONE\_NUMBER)) ) {

                n.number = null;

            }

 

            if (DBG) log("NotificationInfo constructed for number: " + n.number);

 

            return n;

        }

    }