【问题标题】:Read contacts individually from each sim card in a dual sim phone从双卡手机中的每张 SIM 卡中单独读取联系人
【发布时间】:2017-07-22 17:00:43
【问题描述】:

目前我正在使用 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);

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

如果有人能想到一些其他的方法,他们是非常受欢迎的。我真的很感激任何形式的帮助。

【问题讨论】:

    标签: android android-contacts dual-sim


    【解决方案1】:

    您可以使用 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.sim1 和 vnd.sec.contact.sim2。

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

    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: near ".": syntax error (code 1): , while compile: SELECT sort_key, send_to_voicemail, pinned, 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))
    • 是的,我在选择值周围缺少一个撇号,现在已修复
    • 我尝试了您的解决方案,但没有成功。 selection = account_type='vnd.sec.contact.sim' cur = android.content.ContentResolver$CursorWrapperInner@15ebf227 I/System.out: >>>>> 转储光标 android.content.ContentResolver$CursorWrapperInner@15ebf227 I/System.出:
    • @marmor 我有同样的问题,我想在双卡中以编程方式创建联系人,但我不知道如何区分内容 URI,你能建议我吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多