Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting phone number from contacts using ContentProvider - Android

I am making a small app where I can get the contacts of my phone using a content provider and display them in a listview as illustrated.

enter image description here

I want to select a row of the listview and automically make a phone call to that specific contact. I tried some things,but they don't 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.

  }
}
like image 631
Theo Avatar asked Oct 10 '15 08:10

Theo


People also ask

How would you access data in a ContentProvider?

When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider .


2 Answers

First, ensure that you have added the Permission to your AndroidManifest.xml file:

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

UPDATE: On Android 6 and higher stating the permission in the manifest is not enough, you have to explicitly ask user for granting permission on reading contacts otherwise, you will get an exception. See this answer for more details.

Then you can loop through your phone contacts like this:

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();
}
like image 78
Rajan Bhavsar Avatar answered Sep 28 '22 09:09

Rajan Bhavsar


Try with:

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.RAW_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();
}
like image 20
Evin1_ Avatar answered Sep 28 '22 10:09

Evin1_