2

我正在尝试创建一个自定义联系人应用程序,该应用程序仅显示那些具有Contact Number的联系人。首先,有没有自动化的方法来做到这一点?假设不是,那么我正在尝试按其名称搜索联系人,例如Rohan

这是代码:-

Cursor photoCursor = getContentResolver().query(
            android.provider.ContactsContract.Contacts.CONTENT_URI,
            new String[] { ContactsContract.Contacts.PHOTO_ID,
                    ContactsContract.Contacts.DISPLAY_NAME },
            ContactsContract.Contacts.DISPLAY_NAME + " = ?",
            new String[]{"Rohan"}, null);
    photoCursor.moveToFirst();
    while (photoCursor.moveToNext()) {
        Log.d("Photo Thumbnail", "" + photoCursor.getString(1));
    }

尽管联系人存在,但我没有收到任何日志,如果我删除选择选择参数,我会在日志中看到Rohan。我究竟做错了什么?

4

4 回答 4

6

搜索部分显示名称的简单解决方案。

ContentResolver contentResolver = getCurrentActivity().getContentResolver();

String   whereString = "display_name LIKE ?";
String[] whereParams = new String[]{ "%" + searchText + "%" };

Cursor contactCursor = contentResolver.query(
        ContactsContract.Data.CONTENT_URI,
        null,
        whereString,
        whereParams,
        null );

while( contactCursor.moveToNext() ) {

    int contactId = getIntFromCursor( contactCursor, ContactsContract.Data.CONTACT_ID );

    Log.d( "Contact ID", contactId)

}

contactCursor.close();
于 2018-05-10T04:00:04.957 回答
3

我使用以下代码做到了

Cursor cursor = getContentResolver().query(
            android.provider.ContactsContract.Contacts.CONTENT_URI,
            new String[] { ContactsContract.Contacts.PHOTO_ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts._ID },
            ContactsContract.Contacts.HAS_PHONE_NUMBER, null,
            ContactsContract.Contacts.DISPLAY_NAME);

cursor给出了所有具有任何电话号码的联系人,然后我将唯一的保存IDArrayList这样的

cursor.moveToFirst();

    while (cursor.moveToNext()) {
        contactsID.add(cursor.getString(2));
    }

然后在选择联系人时,我使用这个找到联系人号码

Cursor cursor = getContentResolver()
                    .query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            new String[] {
                                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?",
                            new String[] { contactsID.get(position) }, null);
            contactNumbers = new ArrayList<String>();
            while (cursor.moveToNext()) {
                contactNumbers.add(cursor.getString(0));
                Log.d("number", cursor.getString(0));
            }
于 2013-10-07T10:57:43.890 回答
2

尝试这个:

    Cursor contactLookupCursor =
            getContentResolver().query(
                    Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                            Uri.encode("Rohan")),
                    new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER},
                    null,
                    null,
                    null);

    try {
        while (contactLookupCursor.moveToNext()) {
            contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
            contactNumber = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.NUMBER));
        }
    } finally {
        contactLookupCursor.close();
    }
于 2013-09-03T20:48:01.967 回答
1

看起来您正在尝试实现一个允许用户选择联系人的屏幕,然后选择该联系人的电话号码。

如果是这种情况,您可以改用电话选择器意图:

Intent intent = Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);

这将打开本机联系人应用程序,并允许用户选择联系人和电话号码。然后,您将在您的应用程序中收到如下结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
        // Get the URI and query the content provider for the phone number
        Uri contactUri = data.getData();
        String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
        Cursor cursor = getContentResolver().query(contactUri, projection,
                null, null, null);
        // If the cursor returned is valid, get the phone number
        if (cursor != null && cursor.moveToFirst()) {
            int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(numberIndex);
            // Do something with the phone number
            ...
        }
    }
}
于 2021-04-29T12:37:25.850 回答