I used this code, and I can get the number from a contact when clicked
to a contact. However, some contacts have more than one phone number.
How can I access to another cell number. Thanks in advance.

Here is my code:

//Choose number from phone book setup
    btnPhonebook.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent pb = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(pb, 1);
        }
    });

}

//Listen the result from phone book button
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
    ContentResolver cr = getContentResolver();
    if (resultCode == Activity.RESULT_OK) {
        Uri contactData = data.getData();
        Cursor contactCursor = managedQuery(contactData, null, null,
null, null);
        String cellNoFromPhonebook;
        String phoneNumber = null;
        if (contactCursor.moveToFirst()) {
            cellNoFromPhonebook =
contactCursor.getString(contactCursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

            Cursor phones =
getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,null,null, null);
            while (phones.moveToNext())
            {
                String name =
phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                if (name.compareTo(cellNoFromPhonebook) == 0) {
                    phoneNumber =
phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }

            }
            phones.close();

            etNumber.setText(phoneNumber);
        }
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to