Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

java - How to read android sim contacts and phone contacts separately

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...