2017-07-22 115 views
0

目前,我正在使用SubscriptionManager類API來檢測SIM是否存在/不存在。 在單人SIM手機下方的方法工作從SIM卡讀取聯繫人。從雙SIM卡手機中的每張SIM卡分別讀取聯繫人

Uri simUri = Uri.parse("content://icc/adn/"); 
    ContentResolver mContentResolver = this.getContentResolver(); 
    Cursor c = mContentResolver.query(simUri, null, null, null, null); 

我的應用程序是一個系統應用程序,並有一個根設備。 對於雙SIM卡手機如何從每個SIM卡單獨讀取聯繫人。

如果有人能想到其他方法,他們是非常受歡迎的。我真的很感謝這方面的幫助。

回答

0

您可以使用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.sim1vnd.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 
+0

我得到以下異常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

+0

是的,我錯過了選擇值周圍的撇號,現在修復 – marmor

+0

我試過你的解決方案,但沒有運氣。 選擇= ACCOUNT_TYPE = 'vnd.sec.contact.sim' CUR = [email protected] I/System.out的:>>>>>傾倒光標[email protected] 我/系統.out:<<<<< – Pause

相關問題