以下是我的代码,用于从通话记录中读取联系人电话号码,并且我正在调用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,任何人都可以帮助我!

最佳答案

/**
 * 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”之类的数字(无特殊字符)

关于android - Android如何通过我的通话记录联系人电话号码获取联系人ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29121175/

10-13 05:03