moo
moo

Reputation: 95

android showing both sim and phone contacts

In my code I should display only phone contacts: I followed previous posts but I still display both phone and sim contacts. Here it is my code:

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

String columIndex = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
String columIndexId = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String numIndex = ContactsContract.CommonDataKinds.Phone.NUMBER;

Cursor cursor = getContentResolver().query(uri, null, null, null,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");

if(cursor!=null){
    while(cursor.moveToNext()) {
        ContactInfo ci = new ContactInfo();
        ci.setIdContact(Integer.parseInt(cursor.getString(cursor.getColumnIndex(columIndexId))));
        ci.setName(cursor.getString(cursor.getColumnIndex(columIndex)));
        ci.setNumberTel(cursor.getString(cursor.getColumnIndex(numIndex)));
        //if(!cursor.getString(cursor.getColumnIndex(columIndex)).equalsIgnoreCase(nome))
        listContact.add(ci);
    }
    cursor.close();

}

These are all ContactInfo object and they will be showed in a list (listContact, which is an ArrayList).

It is really important for me because my application works good only on phone contacts and not on sim contacts.

Upvotes: 2

Views: 2346

Answers (1)

Kuitsi
Kuitsi

Reputation: 1675

You can try to replace your Cursor with this to exclude contacts on sim:

import android.provider.ContactsContract.RawContacts;

Cursor cursor = getContentResolver().query(
    RawContacts.CONTENT_URI,
    new String[] { RawContacts._ID, RawContacts.ACCOUNT_TYPE },
    RawContacts.ACCOUNT_TYPE + " <> 'com.android.contacts.sim' AND "
        + RawContacts.ACCOUNT_TYPE + " <> 'com.anddroid.contacts.sim' AND " // HTC
        + RawContacts.ACCOUNT_TYPE + " <> 'vnd.sec.contact.sim' AND "
        + RawContacts.ACCOUNT_TYPE + " <> 'USIM Account' ",
        null,
        null);

I haven't tried this, modified from https://stackoverflow.com/a/4409453/262462

Note: on some phones you may need to exclude few other RawContacts.ACCOUNT_TYPE like com.anddroid.contacts.sim and vnd.sec.contact.sim, see https://stackoverflow.com/a/13656077/262462

Upvotes: 3

Related Questions