您可以使用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