Get phone contacts

1. Get the phone contact list

private String[] getContacts() {
        //The Uri of the contact, which is content://com.android.contacts/contacts
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        //Specify to get _id and display_name two columns of data, display_name is the name
        String[] projection = new String[]{
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };
        //Query the corresponding ContentProvider according to Uri, cursor is the obtained dataset
        Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null);
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            Log.d("MainActivity", cursor.getColumnName(i));
        }
        String[] arr = new String[cursor.getCount()];
        int i = 0;
        if (cursor != null && cursor.moveToFirst()) {
            do {
                Long id = cursor.getLong(0);
                //get name
                String name = cursor.getString(1);
                //Specify to get the NUMBER column of data
                String[] phoneProjection = new String[]{
                        ContactsContract.CommonDataKinds.Phone.NUMBER
                };
                arr[i] = id + " , 姓名:" + name;

                //Get the person's phone number based on the contact's ID
                Cursor phonesCusor = this.getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        phoneProjection,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id,
                        null,
                        null);

                //Because each contact may have multiple phone numbers, it needs to be traversed
                if (phonesCusor != null && phonesCusor.moveToFirst()) {
                    do {
                        String num = phonesCusor.getString(0);
                        arr[i] += " , phone number: " + num;
                    } while (phonesCusor.moveToNext());
                }
                i++;
            } while (cursor.moveToNext());
        }
        return arr;
    }

2. Jump to the system contact page, select and bring back the contact

This method is recommended, because some 7.0 and 8.0 mobile phones will not support the form of listing all contacts, and the phone number of the contact cannot be queried after returning.

Uri uri = Uri.parse("content://contacts/people"); // List all contacts in the form
                        Uri uri2 = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // All phone numbers in the form
                        Intent intent = new Intent(Intent.ACTION_PICK, uri2);
                        activity.startActivityForResult(intent, 0);
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (data == null) {
                return;
            }
            //Process the returned data and get the selected contact information
            Are you = data.getData ();
            // select phone number
            getPhoneContacts (s);
        }
    }
@Nullable
    private void getPhoneContacts(Uri uri) {
        contact = new String[2];
        //Get the ContentResolver object
        ContentResolver cr = getContentResolver();
        //Get the cursor of the starting item in the phonebook
        Cursor cursor = cr.query(uri, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            try {
                // get the contact name
                int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                int numberFieldColumnIndex = cursor.getColumnIndex(ContactsContract.Data.DATA1);
                if (BuildConfig.DEBUG)
                    Log.d("SimpleWebActivity", "nameFieldColumnIndex:" + nameFieldColumnIndex);


                contact[0] = cursor.getString(nameFieldColumnIndex);
                contact[1] = cursor.getString(numberFieldColumnIndex);
            } catch (Exception e) {
                // If the user rejects the first permission request in the selection interface
                return;
            }
            cursor.close();
        } else {
            return;
        }
        people(contact[0], contact[1]);
    }




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763578&siteId=291194637