我的AccountManager帐户没有联系人 [英] No contacts for my AccountManager account
问题描述
我目前正在一个我想访问移动通讯录的项目中,所以我设法用accountmanager
创建帐户,并且还能够执行Syncadapter
操作.我可以看到我的帐户是在移动settings->Accounts
中创建的.但是,当我尝试使用下面的代码获取我帐户的所有联系人时,它不起作用.它显示除我的应用程序帐户联系人以外的所有应用程序(google.com和WhatsApp.com)联系人.
I am currently working on a project in which I want to access the mobile contacts, So I have managed to create account with accountmanager
and also able to perform Syncadapter
operation. I could see my account got created in the mobile settings->Accounts
. However, when I try to get all the contacts with my account with below code ,it does not work. Its showing all apps(google.com and WhatsApp.com) contacts except my app account contacts.
Cursor cursor = getContext().getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts.DIRTY, ContactsContract.RawContacts.ACCOUNT_TYPE},
null,
null,
null);
if (cursor != null && cursor.getCount() >0) {
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
Log.d("Dirty",cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.DIRTY)));
Log.d("ACCountType",cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)));
cursor.moveToNext();
}
cursor.close();
}
我不了解的是,我需要创建ContentProvider
并代表我的帐户将所有联系人重新插入Contactsprovider
吗?
What I dont understand is do I need to create ContentProvider
and insert all contacts back to Contactsprovider
on behalf of my account?
推荐答案
不确定您是否完全了解ContactsProvider的工作原理.
Not sure if you've fully understood how the ContactsProvider works.
您应该了解几件事:
- 每个RawContact都唯一地分配给一个特定帐户,它不能属于多个帐户(因此,您的应用通常无法同步现有联系人,因为他们已经有一个帐户).
- 所有应用程序在所有联系人上都具有相同的视图,特别是所有应用程序都可以查看和修改所有联系人(只要他们具有权限),尽管该规则有一些例外.
-
当您将联系人同步到您的帐户时,您必须按照 ContactsContract.RawContacts
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
当您阅读联系人时,除非您指定Uri查询参数或选择,否则您会获得所有帐户的联系人:
When you read contacts you get contacts of all accounts, unless you specify Uri query parameters or a selection:
Uri rawContactUri = RawContacts.CONTENT_URI.buildUpon()
.appendQueryParameter(RawContacts.ACCOUNT_NAME, accountName)
.appendQueryParameter(RawContacts.ACCOUNT_TYPE, accountType)
.build();
Cursor c1 = getContentResolver().query(rawContactUri,
RawContacts.STARRED + "<>0", null, null, null)
此查询返回指定帐户的所有已加星标联系人.
This query returns all starred contacts of the specified account.
如果您的代码用作同步适配器,则还必须添加Uri查询参数 CALLER_IS_SYNC_ADAPTER
,否则许多操作可能会得到不同的结果.
If your code operates as a sync adapter you also have to add the Uri query parameter CALLER_IS_SYNC_ADAPTER
, otherwise you may get different results for many operations.
这篇关于我的AccountManager帐户没有联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!