Anagha Magar
Anagha Magar

Reputation: 21

Retrieve sim card contacts in android programmatically

In my android app I want to retrieve all sim and phone contacts. If I execute the code separately it works fine but if i combine it I just get phone contacts. How to get both contacts ??? My code for sim contacts is :

private void SIMContacts()
{
  try
    {
      String strPhonename = null; 
        String strphoneNo = null;

    Uri simUri = Uri.parse("content://icc/adn"); 
    Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);

    while (cursorSim.moveToNext()) 
    {      
        strPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
        strphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
        strphoneNo.replaceAll("\\D","");
        strphoneNo.replaceAll("&", "");
        strPhonename=strPhonename.replace("|","");

        Log.i("Contact: ", "name: "+strPhonename+" phone: "+strphoneNo);
    }        
}
catch(Exception e)
{
    e.printStackTrace();
}
}

Upvotes: 0

Views: 1405

Answers (1)

Rishi Paul
Rishi Paul

Reputation: 936

Try to use this and get Number List from aa arrayList

public void getNumber(ContentResolver cr) {

    public static ArrayList<String> aa = new ArrayList<String>();
    Cursor phones = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);

    // System.out.println("Phone" + phones.getCount());

    while (phones.moveToNext()) {
        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        // System.out.println(".................." + phoneNumber);
        aa.add(name);
        aa.add(phoneNumber);
    }
    phones.close();// close cursor



    // display contact numbers in the list
}

Upvotes: 1

Related Questions