Getting phone number from contacts using ContentProvider - Android

I am creating a small application where I can get the contacts of my phone using the content provider and display them in a list, as shown in the figure.

enter image description here

I want to select a list line and automatically make a phone call for this particular contact. I tried something, but they do not work. Any ideas? Here is my code.

public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener{
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ContentResolver cr = getContentResolver();
    Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI,
            new String[] {ContactsContract.Contacts.DISPLAY_NAME},
            null, null, null);

    List<String> contacts = new ArrayList<String>();
    if (c.moveToFirst()) {
        do {
            contacts.add(c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        } while (c.moveToNext());
    }
    adapter = new ArrayAdapter<String>(this, R.layout.activity_main, contacts);
    setListAdapter(adapter);



}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

      //The answer should be inside here.

  }
}
+4
source share
2 answers

First make sure you add permission:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

into your AndroidManifest.xml file, then you can scroll through the phone contacts as follows:

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
while (cursor.moveToNext()) { 
    String contactId = cursor.getString(cursor.getColumnIndex( 
    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

    if (Boolean.parseBoolean(hasPhone)) { 
        // You know it has a number so now query it like this
        Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
        while (phones.moveToNext()) { 
            String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));                 
        } 
        phones.close(); 
    }

    Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); 

    while (emails.moveToNext()) { 
        // This would allow you get several email addresses 
        String emailAddress = emails.getString( 
        emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
    } 

    emails.close();
}
+3
source

Try:

private void doMagicContacts() {
    Cursor cursor = getContentResolver()
            .query(ContactsContract.Contacts.CONTENT_URI,
                    null,
                    null,
                    null,
                    null);

    if (cursor == null) {
        return;
    }

    cursor.moveToFirst();

    do {
        String name = cursor.getString(
                cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String id = cursor.getString(
                cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));

        Cursor phones = getContentResolver()
                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
                        null,
                        null);
        if (phones != null) {
            while (phones.moveToNext()) {
                String phoneNumber = phones.getString(
                        phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Log.d(TAG, "doMagicContacts: " + name + " " + phoneNumber);
            }
            phones.close();
        }

    } while (cursor.moveToNext());

    cursor.close();
}
0
source

Source: https://habr.com/ru/post/1611068/


All Articles