0

I am making an app, in that app, the user will speak CALL (person name). But, I am not able to understand how to access contacts and call that person automatically. Till now, I am able to call a particular number only. My code is mentioned below.

if(msg.indexOf("call")!=-1){
    Intent i2=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+"123456789"));
    startActivity(i2);
 }
Shahadat Hossain
  • 533
  • 7
  • 20

2 Answers2

1
 private void getContactList() {
  ContentResolver cr = getContentResolver();
  Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
        null, null, null, null);

if ((cur != null ? cur.getCount() : 0) > 0) {
    while (cur != null && cur.moveToNext()) {
        String id = cur.getString(
                cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(cur.getColumnIndex(
                ContactsContract.Contacts.DISPLAY_NAME));

        if (cur.getInt(cur.getColumnIndex(
                ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
            Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[]{id}, null);
            while (pCur.moveToNext()) {
                String phoneNo = pCur.getString(pCur.getColumnIndex(
                        ContactsContract.CommonDataKinds.Phone.NUMBER));
                Log.i(TAG, "Name: " + name);
                Log.i(TAG, "Phone Number: " + phoneNo);
            }
            pCur.close();
        }
    }
}
if(cur!=null){
    cur.close();
    }
}

You can refer this stackoverflow links:

android get all contacts

http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html

https://developer.android.com/training/contacts-provider/retrieve-names

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Mayur Coceptioni
  • 433
  • 4
  • 17
1

You can use below library to fetch contacts

implementation 'com.github.tamir7.contacts:contacts:1.1.7'

https://github.com/tamir7/Contacts