您可以使用ContactsContract API的ACCOUNT_TYPE字段查找SIM联系人。
您需要通过RawContacts表查询,以获取存储在SIM卡上的所有RawContacts列表,并从该列表中获取联系人ID。
String selection = RawContacts.ACCOUNT_TYPE + "='vnd.sec.contact.sim'";
Cursor cur = mContentResolver.query(RawContacts.CONTENT_URI, null, selection, null, null);
// cur should now contain all the requested contacts
DatabaseUtils.dumpCursor(cur);
UPDATE
因为这没有工作,这意味着存储的ACCOUNT_TYPE
的值并不如预期,可能像vnd.sec.contact.sim1
和vnd.sec.contact.sim2
。
您可以创建一个测试方法来打印所有联系人的所有ACCOUNT_TYPE
的值,并使用该输出找出您需要的正确值。
HashSet<String> accountTypes = new HashSet<String>();
String[] projection = new String[] { RawContacts.ACCOUNT_TYPE };
Cursor cur = mContentResolver.query(RawContacts.CONTENT_URI, projection, null, null, null);
if (cur != null) {
while (cur.moveToNext()) {
accountTypes.add(cur.getString(0));
}
}
for (String type : accountTypes) {
Log.d("ACCOUNT_TYPE", type);
}
// Check the output in LogCat and look for `ACCOUNT_TYPE`s that look like `sim` types. then add those values to the selection of the above method
我得到以下异常android.database.sqlite.SQLiteException:近 “”:语法错误(代码1):在编译:SELECT sort_key,send_to_voicemail,固定,sec_custom_alert,DISPLAY_NAME,display_name_alt AS phonetic_name_style FROM view_raw_contacts_restricted WHERE(1)AND((account_type = vnd.sec.contact.sim)) – Pause
是的,我错过了选择值周围的撇号,现在修复 – marmor
我试过你的解决方案,但没有运气。 选择= ACCOUNT_TYPE = 'vnd.sec.contact.sim' CUR = [email protected] I/System.out的:>>>>>倾倒光标[email protected] 我/系统.out:<<<<< – Pause