简体   繁体   中英

Android provide/sync contacts from app

I would like to save and sync contacts with the phone Contacts. The saved contacts from my app should appear under some syncaccount. If the app is uninstalled, then all these contacts should be removed.

After reading the Android documentation, I have created a SyncAdapter and ContentProvider. The only thing that these two are doing now is creating a account. My ContentProvider is yet a dummy.

Android联系人帐户

The first thing I would like to do is manually saving a contact by using my own sync account. The following code I have found on SO is only creating a phone contact without any link to my sync account.

public static boolean insertContact(ContentResolver contactAdder,
        String firstName, String mobileNumber) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
            .build());
    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(
                    ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(
                    ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                    firstName).build());
    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(
                    ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                    mobileNumber)
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                    Phone.TYPE_MOBILE).build());

    try {
            contactAdder.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
        return false;
    }

    return true;
}

How can I save a contact from my app under my own syncaccount so you can see my app logo in the Contacts list?

It's kinda hard to find some information about it on internet. Most resuls are about exchanging/modifying existing contacts.

I have managed to save a contact under my sync account!

I was so close. It was just this giving the account type and name while doing a insert:

    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, MY_ACCOUNT_TYPE)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, MY_ACCOUNT_NAME)
            .build());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM