Android ContactsContract类:如何忽略非主要ACCOUNT_TYPES? [英] Android ContactsContract class: How to ignore non-primary ACCOUNT_TYPES?

查看:166
本文介绍了Android ContactsContract类:如何忽略非主要ACCOUNT_TYPES?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我现在知道我可以使用ContactsContract类列出android设备上可用的所有联系人.像这样:

So I know by now that I can use ContactsContract class to list all contacts available on an android device. Something like this:

private void getContacts(){

ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(ContactsContract.contacts.CONTENT_URI,null,null,null,null);

while(cursor.moveToNext){

//get contact id
.....
//get contact name
....

}

}

上面的contact是什么意思:

What do I mean by contact above:

一个contact是一组raw_contacts.示例:

电话簿中有2个contacts:

[ User A  ]
----------- 
[ User B  ]

点击用户A后,我将得到此信息:

Upon clicking User A I will get this:

   | User A                                             |
   | phone 1: 0000 mobile                               | 
   | phone 2: 1111 home                                 |
   | phone 3: 2222 work                                 |
   |                                                    |
   |        linked :google , sim, phone, viber, whatsapp|

据我了解:

  • 联系人=用户A或用户B.

  • Contacts = User A or User B.

raw_contacts =用户A(电话)或用户A(SIM)或用户A(谷歌)或用户A(viber)....

raw_contacts = User A (phone) or User A (SIM) or User A (google) or User A (viber)....

我的问题是:

如果我遍历所有contacts,然后在contact 中遍历所有raw_contacts,请记住raw_contacts可能很多,然后遍历电话号码(家庭,移动电话,工作...)的每个原始联系人...那么对性能不会不好吗?

If I looped through all contacts and then looped through all raw_contacts in a contact keeping in mind that raw_contacts can be alot, and then looped through phone number (home, mobile, work...) of each raw contact...Then wouldn't it be bad for performance?

我该怎么做才能只循环浏览存储在手机(SIM卡或设备)上的手机号码,而不必循环浏览自定义应用程序生成的raw_contacts?

What should I do to only loop through mobile numbers that are stored on phone (sim, or device) without having to loop through raw_contacts that are generated by custom apps?

遍历所有raw_contacts没有意义.

诸如whatsapp或viber或电报之类的应用程序或任何电话应用程序都能快速有效地获取这些联系人.

Apps like whatsapp or viber or telegram or any phone app get these contacts fast and efficiently.

谢谢.

推荐答案

...那么性能会不会很糟糕?

...Then wouldn't it be bad for performance?

绝对不好的表现.

我该怎么做才能仅遍历手机号码?

What should I do to only loop through mobile numbers?

直接在ContactsContract.Data表上进行迭代,以从所有RawContacts获取所有电话.

Iterate directly over the ContactsContract.Data table, to get all phones from all RawContacts.

诸如whatsapp或viber或电报之类的应用程序或任何电话应用程序都可以 快速有效地联系.

Apps like whatsapp or viber or telegram or any phone app get these contacts fast and efficiently.

这在某种程度上是错误的,它看起来好像是超快的,因为这些应用程序运行服务来查询联系人,与服务器进行通信,然后在本地缓存结果.然后,他们仅需要定期查询增量(已添加/已删除的联系人).即使使用以下更快的代码,也要在后台线程中运行它,因为运行时间可能会长一些,具体取决于设备上的联系人数量.

This is partially false, it seems like it's super-fast because those app run a service to query for contacts, communicate with the server, and then cache results locally. Then they only need to query periodically for the delta (contacts added/removed). Even with the faster code below, run this in a background thread, as it may take time to run depending on the number of contacts on the device.

示例代码:

private class ContactInfo {
    public long id;
    public String name;
    Set<String> phones = new HashSet<>();

    public ContactInfo(long id, String name) {
        this.id = id;
        this.name = name;
    }
}

Map<Long, ContactInfo> contacts = new HashMap<Long, ContactInfo>();

String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.DATA1};

// limit data results to just phone numbers.
// Note: you can potentially restrict the query to just phone & SIM contacts,
// but that would actually make the query slower not faster, because you'll need multiple queries over the DB, 
// instead of just a big one.
String selection = Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'";

Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);

while (cur.moveToNext()) {
    long id = cur.getLong(0);
    String name = cur.getString(1);
    String data = cur.getString(2); // the actual info, e.g. +1-212-555-1234

    Log.d(TAG, "got " + id + ", " + name + ", " + data;

    // add found phone to existing ContactInfo, or create a new ContactInfo object
    ContactInfo info;
    if (contacts.containsKey(id)) {
        info = contacts.get(id);
    } else {
        info = new ContactInfo(id, name);
        contacts.put(id, info);
    }
    info.phones.add(data);
}
cur.close();

// you now have a mapping between contact-id to an info object containing id, name, and a list of phone-numbers!

这篇关于Android ContactsContract类:如何忽略非主要ACCOUNT_TYPES?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆