2013-02-16 35 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