How to query all contacts that belong to an account?

百般思念 提交于 2019-12-04 07:10:37

The main issue I see that can explain why users won't see results from your code, is that you're assuming all the contacts are stored on a Google account.

While this is the default behavior in some devices, it's not the default on all devices, also, users can freely change their contacts storage to any other location (yahoo contacts, MS exchange, phone-only (unsynced), etc.)

Having that said, if your only requirement is to

fetch all contacts that have a specified account.

I think that's a much better alternative then your 2 queries (one of which runs over all contacts, not just the required ones):

// Uri to query contacts that have a RawContact in the desired account
final Uri.Builder builder = Contacts.CONTENT_URI.buildUpon();
builder.appendQueryParameter(RawContacts.ACCOUNT_NAME, whatsappAccountName);
builder.appendQueryParameter(RawContacts.ACCOUNT_TYPE, whatsappAccountType);
Uri uri = builder.build();

String[] projection = new String[]{ Contacts.LOOKUP_KEY, Contacts._ID Contacts.DISPLAY_NAME }; // add more if needed

// boo-yaa!
Cursor cur = cr.query(uri, projection, null, null, null);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!