Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read android sim contacts and phone contacts separately

Tags:

java

android

I want to read all sim contacts and phone contacts separately in Android. I searched for this and found lots of people having problem with this and I couldn't find any solution yet. I fond some answer here, but it doesn't work for me. When I test this it give me google contacts:

RawContacts.ACCOUNT_TYPE + " = 'com.google' "

But when I test this it does not give me sim contacts:

RawContacts.ACCOUNT_TYPE + " = 'com.android.contacts.sim' "

Then I found RawContacts are contacts that created by sync adapter in here. That may be the problem. So can anyone tell me the way of getting

  • All simcontacts
  • All phone contacts

Thanks.

like image 875
sampathpremarathna Avatar asked Jan 18 '12 10:01

sampathpremarathna


2 Answers

for Phone contacts

try
{
    String[] PROJECTION=new String[] {Contacts._ID,
        Contacts.DISPLAY_NAME,
        Phone.NUMBER
    };

    Cursor c=managedQuery(Phone.CONTENT_URI,
        PROJECTION, null, null, null);
        if (c.moveToFirst()) {
            String ClsPhonename = null;
            String ClsphoneNo = null;

            do 
            {
                ClsPhonename = c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME));
                ClsphoneNo = c.getString(c.getColumnIndex(Phone.NUMBER));


                ClsphoneNo.replaceAll("\\D", "");
                ClsPhonename=ClsPhonename.replaceAll("&", "");
                ClsPhonename.replace("|","");
                String ClsPhoneName=ClsPhonename.replace("|","");

                }   

            } while(c.moveToNext());
        }

for sim contacts

String ClsSimPhonename = null; String ClsSimphoneNo = null;

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

    while (cursorSim.moveToNext()) 
    {      
        ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
        ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
        ClsSimphoneNo.replaceAll("\\D","");
        ClsSimphoneNo.replaceAll("&", "");
        ClsSimPhonename=ClsSimPhonename.replace("|","");
            System.out.println("SimContacts"+ClsSimPhonename);
            System.out.println("SimContactsNo"+ClsSimphoneNo);
            dts.createDatabse("MyCellFamily",getApplicationContext());

    }        
}
catch(Exception e)
{
    e.printStackTrace();
}
like image 161
bindal Avatar answered Sep 22 '22 07:09

bindal


I found many phones use "com.android.contacts.sim" in RawContacts.ACCOUNT_TYPE for sim contacts. But I also found that HTC use "com.anddroid.contacts.sim" (misspelled for android). That is weird.

like image 37
Masson Avatar answered Sep 19 '22 07:09

Masson