0

I have used this code to display the name and the phone number of my phone even though it is returning the name only, I would appreciate any answer. does it also works if i have google account synced?

CODE: public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (people != null && people.moveToFirst()) {
        do {
            try {
                int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                String contact = people.getString(nameFieldColumnIndex);
                Log.d("CONTACT: ", contact);
                int numberFieldColumnIndex = people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
                String number = people.getString(numberFieldColumnIndex);
                Log.d("NUMBER: ", number);
            } catch (Exception e) {
                Log.e("ERROR: ", e.toString());
            }

        } while (people.moveToNext());
    }
}

ERROR:

  04-16 05:16:27.839    1426-1426/com.chatter.contactsaccess D/CONTACT:﹕ Test
04-16 05:16:27.839    1426-1426/com.chatter.contactsaccess D/NUMBER:﹕ 1

EDITED

kobbycoder
  • 682
  • 2
  • 10
  • 33

3 Answers3

1

You can try the below code to get all the phone contacts in your phone.

In onCreate of your Activity pass the intent to open the phone contacts like,

private static final int PICK_CONTACT=1;

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                    startActivityForResult(intent, PICK_CONTACT);

And get the details of the selected contact in onAcivityResult like below

public void onActivityResult(int reqCode, int resultCode, Intent data) {
        if(reqCode==0)return;
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
            case (PICK_CONTACT) :
                if (resultCode == Activity.RESULT_OK) {

                    Uri contactData = data.getData();
                    ContentResolver cr=getActivity().getContentResolver();
                    final Cursor c =  cr.query(contactData, null, null, null, null);

                    if (c.moveToFirst()) {
                        ArrayList<String> alNumbers=new ArrayList<>();


                        String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                        String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                        if (hasPhone.equalsIgnoreCase("1")) {
                            Cursor phones = getActivity().getContentResolver().query(
                                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
                                    null, null);
                            if(phones.moveToFirst())
                            {
                                while(!phones.isAfterLast())
                                {
                                    cNumber = phones.getString(phones.getColumnIndex("data1"));
                                    alNumbers.add(cNumber);
                                    phones.moveToNext();
                                }
                            }
                            phones.close();
                            final CharSequence[] items = alNumbers.toArray(new String[alNumbers.size()]);
                            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                            builder.setTitle("Choose a number");
                            builder.setItems(items, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int item) {
                                    cNumber = items[item].toString();
                                    name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                                    phone.setText(name+" ("+cNumber+")");
                                }
                            });
                            AlertDialog alert = builder.create();
                            if(alNumbers.size() > 1) {
                                alert.show();



                               }
 name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
Anusha Harish
  • 382
  • 3
  • 14
0

There is no

ContactsContract.PhoneLookup.NUMBER

present in

ContactsContract.Contacts.CONTENT_URI

, use

ContactsContract.Contacts.HAS_PHONE_NUMBER

http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html

Try this way,

String name = c=.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Community
  • 1
  • 1
Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39
0

I fixed it by changing ContactsContract.Contacts.CONTENT_URI for ContactsContract.CommonDataKinds.Phone.CONTENT_URI

the snipped code is the following (I just want to get the name and phone in string):

Cursor people = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        while(people.moveToNext()) {
            int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            String contact = people.getString(nameFieldColumnIndex);
            int numberFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String number = people.getString(numberFieldColumnIndex);
            Log.e("CONTACT: ", contact + " " + number);
        }

        people.close();
kobbycoder
  • 682
  • 2
  • 10
  • 33