1

I just created an application, and I managed to get the phone contacts when I press a button and retrieve the number of the selected one.

My problem is that I only see phone contacts and not SIM contacts.

My code is:

@Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
        if (resultCode!=0){
             Uri uri = data.getData();
             Cursor cursor=this.getContentResolver().query(uri, null, null, null, null);

                while (cursor.moveToNext()) { 
                String contactId = cursor.getString(cursor.getColumnIndex( 
                  ContactsContract.Contacts._ID)); 
                String hasPhone = cursor.getString(cursor.getColumnIndex( 
                  ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                if ((Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)) { 
                             // You know have the 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()) { 

                  phoneNumber = phones.getString( 
                   phones.getColumnIndex( 
                      ContactsContract.CommonDataKinds.Phone.NUMBER));

                 } 
                 phonenumber.setText(phoneNumber);

                 phones.close(); 
                } 
              }

        }


    }

And something i cannot understand.

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

Shows all contacts but i cannot pick one and for result

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

Shows only contacts in phone why is that ?

user878813
  • 785
  • 2
  • 16
  • 28
  • See the answer to this previously posted question: http://stackoverflow.com/questions/8908859/how-to-read-android-sim-contacts-and-phone-contacts-separately – Avery Jun 27 '12 at 15:00
  • this wont help i just cannot understand with when i start the intent with no activity for result i get all the contacts and when i do with result i get only the phoneones ?? – user878813 Jun 27 '12 at 18:45

1 Answers1

0

add this in your intent:

intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);

all other is fine.

Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46