Retrieving a Phone Number from Contacts Provider with ID, Android

前端 未结 2 899
梦毁少年i
梦毁少年i 2021-01-23 11:28

I am able to retrieve the contact ID, but then later I wish to separately retrieve the phone number based on the contact ID. The code below is returning a null result for the p

2条回答
  •  猫巷女王i
    2021-01-23 11:56

    // You can fetch the Contact Number and Email With Following Methods.
    String phone = getPhoneNumber(ContactId);
    String email = getEmail("" + ContactId);
    
    private String getPhoneNumber(long id) {
            String phone = null;
            Cursor phonesCursor = null;
            phonesCursor = queryPhoneNumbers(id);
            if (phonesCursor == null || phonesCursor.getCount() == 0) {
                // No valid number
                //signalError();
                return null;
            } else if (phonesCursor.getCount() == 1) {
                // only one number, call it.
                phone = phonesCursor.getString(phonesCursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            } else {
                phonesCursor.moveToPosition(-1);
                while (phonesCursor.moveToNext()) {
    
                    // Found super primary, call it.
                    phone = phonesCursor.getString(phonesCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    break;
    
                }
            }
    
            return phone;
        }
    
        private Cursor queryPhoneNumbers(long contactId) {
            ContentResolver cr = getContentResolver();
            Uri baseUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
                    contactId);
            Uri dataUri = Uri.withAppendedPath(baseUri,
                    ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
    
            Cursor c = cr.query(dataUri, new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.NUMBER,
                            ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY, ContactsContract.RawContacts.ACCOUNT_TYPE,
                            ContactsContract.CommonDataKinds.Phone.TYPE,
                            ContactsContract.CommonDataKinds.Phone.LABEL},
                    ContactsContract.Data.MIMETYPE + "=?",
                    new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE}, null);
            if (c != null && c.moveToFirst()) {
                return c;
            }
            return null;
        }
    
    
        private String getEmail(String id) {
            String email = "";
            ContentResolver cr = getContentResolver();
            Cursor emailCur = cr.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                    new String[]{id}, null);
            while (emailCur.moveToNext()) {
                // This would allow you get several email addresses
                // if the email addresses were stored in an array
                email = emailCur.getString(
                        emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
    //          String emailType = emailCur.getString(
    //                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
            }
            emailCur.close();
            return email;
        }
    

提交回复
热议问题