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();
}
What I dont understand is do I need to create ContentProvider
and insert all contacts back to Contactsprovider
on behalf of my account?
Not sure if you've fully understood how the ContactsProvider works.
There are a few things that you should know:
When you sync a contact to your account you must specify your account as shown on ContactsContract.RawContacts
When you read contacts you get contacts of all accounts, unless you specify Uri query parameters or a selection:
This query returns all starred contacts of the specified account.
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.