Android - 如何获取已保存的联系人ID

时间:2015-05-14 10:47:01

标签: android contacts android-contacts contactscontract

我想获取保存的联系人ID,我使用以下方式检索它,但问题是它返回了错误的ID。

public static String addContact(FragmentActivity activity, String displayname, String mobilenumber, String homeemail) {
    String DisplayName = displayname;
    String MobileNumber = mobilenumber;
    String homeemailID = homeemail;
    int contactID = 0;
    ArrayList<ContentProviderOperation> contentProviderOperation = new ArrayList<ContentProviderOperation>();

    contentProviderOperation.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

    // ------------------------------------------------------ Names
    if (DisplayName != null) {
        contentProviderOperation.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, DisplayName).build());
    }

    // ------------------------------------------------------ Mobile Number
    if (MobileNumber != null) {
        contentProviderOperation.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, MobileNumber)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());
    }

    // ------------------------------------------------------ homeEmail
    if (homeemailID != null) {
        contentProviderOperation.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Email.DATA, homeemailID)
                .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME).build());
    }

    // Asking the Contact provider to create a new contact
    try {
        // activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY,
        // contentProviderOperation);
        ContentProviderResult[] res = activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, contentProviderOperation);
        Uri myContactUri = res[0].uri;
        int lastSlash = myContactUri.toString().lastIndexOf("/");
        int length = myContactUri.toString().length();
        contactID = Integer.parseInt((String) myContactUri.toString().subSequence(lastSlash + 1, length));

    } catch (Exception e) {
        e.printStackTrace();
    }
    return contactID;
}

我认为它传递了错误的联系人数据uri。请帮助我获得正确的联系人ID。

2 个答案:

答案 0 :(得分:1)

尝试这种方式:

Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

// query time
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

if (cursor.moveToFirst()) {

   // Get values from contacts database:
   contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
   name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}

答案 1 :(得分:0)

您不需要以这种方式获取ID。 Uri课程中有getLastPathSegment()方法。请改用

Uri myContactUri = res[0].uri;
contactID = Ineger.parseInt(myContactUri.getLastPathSegment());

除此之外,这是获取已保存联系人ID的正确方法。