`

在Android中根据联系人查询电话号码

阅读更多


详见在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;

        }

    }

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics