2

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
moo
  • 95
  • 4

1 Answers1

3

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

Community
  • 1
  • 1
Kuitsi
  • 1,675
  • 2
  • 28
  • 48
  • Hi, i tried, but as you said there are different Account_type for sim contacts, so you are not able to exclude them with the same code for all phones. anyway thanks for your answer! – moo Mar 08 '13 at 15:20
  • @moo I edited my answer to include multiple ACCOUNT_TYPEs. If you need to exclude even more of them, just add them using the same logic as here. – Kuitsi Mar 11 '13 at 12:17
  • Do you know these values for the phone contacts as well? I currently only know `vnd.sec.contact.phone` and `com.sonyericsson.localcontacts`... – prom85 May 28 '15 at 09:22