2013-02-16 58 views
2

在我的代码中,我应该只显示手机通讯录:我跟着以前的帖子,但我仍然显示手机和SIM卡的联系人。这是我的代码:android同时显示模拟和手机通讯录

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

} 

这些都是的ContactInfo对象,他们将在一个列表(listContact,这是一个ArrayList)来显示。

,因为我的应用程序仅适用于手机通讯录,而不是SIM卡通讯录好这对我来说真的很重要。

回答

3

你可以尝试用它来取代你的光标以排除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); 

我没试过,从https://stackoverflow.com/a/4409453/262462

注意修改:在某些手机上,您可能需要排除其他一些RawContacts.ACCOUNT_TYPEcom.anddroid.contacts.simvnd.sec.contact.sim,看到https://stackoverflow.com/a/13656077/262462

+0

嗨,我试过了,但正如你说,目前有SIM卡联系人不同ACCOUNT_TYPE,所以你不能用相同的代码F排除它们或所有电话。无论如何感谢您的答案! – moo 2013-03-08 15:20:18

+0

@moo我编辑了我的答案以包含多个ACCOUNT_TYPE。如果您需要排除更多,只需使用与此处相同的逻辑添加它们即可。 – Kuitsi 2013-03-11 12:17:49

+0

您是否也知道手机通讯录的这些值?我目前只知道'vnd.sec.contact.phone'和'com.sonyericsson.localcontacts' ... – prom85 2015-05-28 09:22:56