在双卡手机中从每张SIM卡单独读取联系人

时间:2017-07-22 17:00:44

标签: android android-contacts dual-sim

目前我正在使用SubscriptionManager类api来检测sim是否存在。 在下面的单个sim手机中,方法用于读取SIM卡的联系方式。

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

我的应用程序是一个系统应用程序,并且有一个root设备。 对于双卡手机,如何分别从每张SIM卡中读取联系人。

如果有人能想到其他方式,我们非常欢迎。我真的很感激这方面的任何帮助。

1 个答案:

答案 0 :(得分: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);

<强>更新

由于这不起作用,这意味着为ACCOUNT_TYPE存储的值不符合预期,可能类似于vnd.sec.contact.sim1vnd.sec.contact.sim2

您可以创建一个测试方法来打印所有联系人的ACCOUNT_TYPE s的所有值,并使用该输出找出所需的正确值。

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