如何使用ContentResolver获取帐户类型?

时间:2017-10-15 17:41:28

标签: android android-contacts android-contentresolver

如何使用ContentResolver检索帐户类型(whatsapp,viber等)?
这是我的代码

ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Log.d("log : ","name : " + name + ", ID : " + id);

                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String phone = pCur.getString(
                            pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String phone1 = pCur.getString(
                            pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    Log.d("log : ","phone" + phone + " type : "+phone1);
                }
                pCur.close();

1 个答案:

答案 0 :(得分:1)

Android的联系人中的数据分为3个主要表格:

  1. Contacts - 每个条目代表一个联系人,并将一个或多个RawContacts
  2. 组合在一起
  3. RawContacts - 每个条目代表有关某些SyncAdapter(例如Whatsapp,Google,Facebook,Viber)同步的联系人的数据,此组合包含多个数据条目
  4. Data - 有关联系人,电子邮件,电话等的实际数据,每一行都是属于单个RawContact的单个数据
  5. 您的代码的问题在于您浏览了所有Contacts,然后查询每个联系人的电话。您没有查询包含所需RawContactACCOUNT_NAME的{​​{1}}表格。另外,为了获得设备上的所有手机而进行数百次查询效率非常低,您可以使用单个查询来完成此操作。

    ACCOUNT_TYPE

    <强> P.S。 您可以使用// First create a mapping from RAW_CONTACT_ID to ACCOUNT_TYPE (e.g. Whatsapp) Map<Long, String> rawContacts = new HashMap<Long, String>(); String[] projection1 = {RawContacts._ID, RawContacts.ACCOUNT_TYPE}; Cursor cur1 = cr.query(RawContacts.CONTENT_URI, projection1, null, null, null); while (cur1 != null && cur1.moveToNext()) { Long id = cur1.getLong(0); String account = cur1.getString(1); rawContacts.put(id, account); } // Now query for all the PHONES on the device (no need to query the Contacts table at all) String[] projection2 = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, Phone.RAW_CONTACT_ID }; Cursor cur2 = cr.query(Phone.CONTENT_URI, projection2, null, null, null); while (cur2 != null && cur2.moveToNext()) { long id = cur2.getLong(0); String name = cur2.getString(1); String number = cur2.getString(2); // the actual info, e.g. +1-212-555-1234 int type = cur2.getInt(3); // a numeric value representing type: e.g. home / office / personal long raw = cur2.getLong(4); Log.d(TAG, "contact: " + id + ", name: " + name + ", number: " + number + ", type: " + type + ", raw-id: " + raw + ", account-type: " + rawContacts.get(raw)); } 创建第二个HashMap作为关键字,以便对每个联系人的单个联系人的所有信息进行分组。