Android如何通过我的通话记录联系电话号码获取联系人ID

时间:2015-03-18 11:45:11

标签: android

以下是我的代码,用于从通话记录中读取联系电话号码,我通过传递电话号码调用getContactID方法获取联系人ID。该方法必须返回保存在电话簿中的联系人ID。

public void getCallDetails()
{
    @SuppressWarnings("deprecation")
    String sortOrder = String.format("%s limit 100 ", CallLog.Calls.DATE + " DESC");
    Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI, null, null, null, sortOrder);
    int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
    int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
    int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);

    while (managedCursor.moveToNext())
    {

        phoneNumber = managedCursor.getString(number);
        callType = managedCursor.getString(type);
        callDate = managedCursor.getString(date);
        contactName = getContactname(phoneNumber);  
        /**
         * Hrer i am calling getContactID method to get contact id by passing phone number
        */
        contactId = getContactId(phoneNumber);
        //callDateTime = new Date(Long.valueOf(callDate));
        long seconds=Long.parseLong(callDate);
        SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy  hh:mm a");
        callDateTime = format1.format(new Date(seconds));

        callDuration = managedCursor.getString(duration);

        String cType = null;

        int cTypeCode = Integer.parseInt(callType);

        switch(cTypeCode){

           case CallLog.Calls.OUTGOING_TYPE:
               cType = "OUTGOING";
               break;

           case CallLog.Calls.INCOMING_TYPE:
               cType= "INCOMING";
               break;

           case CallLog.Calls.MISSED_TYPE:
               cType = "MISSED";
               break;
            }

        CallData calldata=new CallData(cType, phoneNumber, contactName, callDateTime, callDuration);
        list.add(calldata);
    }

   // managedCursor.close();

}
 /**
  * This method gives the contact id of the saved contact
  * @param phoneNumber2
  * @return contact id
  */
 private int getContactId(String phoneNumber2) {
    // TODO Auto-generated method stub

        return null;
}

/**
  * this method is used to get the contact name by its phone number
  * @param phoneNumber2
  * @return contact name
  */
 private String getContactname(String phoneNumber2) {
    // TODO Auto-generated method stub
     ContentResolver cr = context.getContentResolver();
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
        if (cursor == null) {
            return null;
        }
        String contactName = null;
        if(cursor.moveToFirst()) {
            contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
        }

        if(cursor != null && !cursor.isClosed()) {
            cursor.close();
        }

        return contactName;
}

我不知道如何从其电话号码中获取联系人ID,任何人都可以帮助我!

1 个答案:

答案 0 :(得分:2)

/**
 * Gets a list of contact ids that is pointed at the passed contact number
 * parameter
 * 
 * @param contactNo
 *            contact number whose contact Id is requested (no special chars)
 * @param cxt
 *            application context
 * @return String representation of a list of contact ids pointing to the
 *         contact in this format 'ID1','ID2','34','65','12','17'...
 */
public static String getContactRowIDLookupList(String contactNo, Context cxt) {

    String contactNumber = Uri.encode(contactNo);
    String contactIdList = new String();
    if (contactNumber != null) {
        Cursor contactLookupCursor = cxt.getContentResolver().query(
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                        Uri.encode(contactNumber)),
                new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
                null, null, null);
        if (contactLookupCursor != null) {
            while (contactLookupCursor.moveToNext()) {
                int phoneContactID = contactLookupCursor
                        .getInt(contactLookupCursor
                                .getColumnIndexOrThrow(PhoneLookup._ID));
                if (phoneContactID > 0) {
                    contactIdList += "'" + phoneContactID + "',";
                }
            }
            if (contactIdList.endsWith(",")) {
                contactIdList = contactIdList.substring(0,
                        contactIdList.length() - 1);
            }
        }
        contactLookupCursor.close();
    }
    return contactIdList;
}

如果电话簿中有多个联系人使用相同的联系号码保存,则此方法返回ID列表。您需要做的就是使用此代码并确保您传递给此方法的联系号码仅为数字,如“14085555555”,而不是“+ 1-408(555)-55-55”(无特殊字符)