0

I'm trying to get phone number as string in android, I succeeded to get contact and get from him the phone number but the result in the logs is data1 and the number is 32821. I don't get my problem.

Here is my code:

public void getContact(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 10);
    }

    Intent contactsIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    this.pickContact = 1;
    startActivityForResult(contactsIntent, this.pickContact);
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    if(reqCode == this.pickContact) {
        if (resultCode == Activity.RESULT_OK) {
            Log.d("ContactsH", "ResOK");
            Uri contactData = data.getData();
            Cursor contact =  getContentResolver().query(contactData, null, null, null, null);

            if (contact.moveToFirst()) {
                String phoneNumber = ContactsContract.CommonDataKinds.Phone.NUMBER;

                Log.d("ContactsH", "Calling to:"+phoneNumber);
                contact.close();
                this.callByNumber(phoneNumber);
            }
        }
    } else {
        Log.d("ContactsH", "Canceled");
    }
}

Any help ?

noam aghai
  • 1,364
  • 3
  • 18
  • 30
  • 4
    Possible duplicate of [How to get contacts' phone number in Android](https://stackoverflow.com/questions/11218845/how-to-get-contacts-phone-number-in-android) – Levon Petrosyan Aug 28 '17 at 09:04
  • Did `String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));` not give you the name? Did you not try the same thing for the number? – OneCricketeer Aug 28 '17 at 09:08
  • I'm sorry but this is not working for me, because it is showing the same number not matter who i choose. – noam aghai Aug 28 '17 at 09:15
  • yes I did try but it give me exeption – noam aghai Aug 28 '17 at 09:16
  • @cricket_007 I'm sorry but can you write me specifically how do you do it ? – noam aghai Aug 28 '17 at 09:27
  • I mean replace `ContactsContract.Contacts.DISPLAY_NAME` with `phoneNumber`. If you get the same number, check the other information like the name – OneCricketeer Aug 28 '17 at 09:30

1 Answers1

3

Thanks for @Levon Petrosyan

But I just need to add the part from his link and copy it to my function.

This is the working code:

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
    super.onActivityResult(reqCode, resultCode, data);

    if(reqCode == this.pickContact){
        if (resultCode == Activity.RESULT_OK) {
            Log.d("ContactsH", "ResOK");
            Uri contactData = data.getData();
            Cursor contact =  getContentResolver().query(contactData, null, null, null, null);

            if (contact.moveToFirst()) {
                String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                // TODO Whatever you want to do with the selected contact's name.

                ContentResolver cr = getContentResolver();
                Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                        "DISPLAY_NAME = '" + name + "'", null, null);
                if (cursor.moveToFirst()) {
                    String contactId =
                            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    //
                    //  Get all phone numbers.
                    //
                    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                    while (phones.moveToNext()) {
                        String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                        switch (type) {
                            case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                // do something with the Home number here...
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                // do something with the Mobile number here...
                                Log.d("ContactsH", number);
                                this.callByNumber(number);
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                // do something with the Work number here...
                                break;
                        }
                    }
                    phones.close();
                }
                cursor.close();
            }
        }
    }else{
        Log.d("ContactsH", "Canceled");
    }
}
noam aghai
  • 1,364
  • 3
  • 18
  • 30