Android - 以编程方式同步联系人添加到谷歌帐户帐户、联系人、方式、Android

2023-09-07 16:30:54 作者:初心不改

在我的应用中,我需要将联系人添加到默认谷歌帐户并同步它.

In my app I need to add contact to default google account and sync it.

这是我的代码:

public static void addContact(Context context, String DisplayName,String WorkNumber, String MobileNumber, String emailID,
                                     String jobTitle, String company, String address){


    ArrayList <ContentProviderOperation> ops = new ArrayList < ContentProviderOperation > ();
    String account = getUsernameLong(context);

    ops.add(ContentProviderOperation.newInsert(
            ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google")
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account)

            .build());

    //------------------------------------------------------ Names
    if (DisplayName != null) {
        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.DISPLAY_NAME,
                        DisplayName).build());
    }

    ..................

    try {
        context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        //requestSyncNow(context);
    } catch (Exception e) {
        e.printStackTrace();

                try {
                    //Toast.makeText(context, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (Exception e1) {

                }
    }
}

这里的函数getUsernameLong()返回google账号

Here function getUsernameLong () that return google account

public static String getUsernameLong(Context context) {
                AccountManager manager = AccountManager.get(context);
                Account[] accounts = manager.getAccountsByType("com.google");
                List<String> possibleEmails = new LinkedList<String>();

                for (Account account : accounts) {

                    // account.name as an email address only for certain account.type values.
                    possibleEmails.add(account.name);
                    Log.i("DGEN ACCOUNT","CALENDAR LIST ACCOUNT/"+account.name);
                }

                if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
                    String email = possibleEmails.get(0);
                    return email;

                }
                return null;
            }

此代码将姓名添加到联系人和电话上,我可以在电话上看到它在 xxx@gmail.com 帐户上,但它与远程帐户不同步.我在 gmail 帐户作为联系人或在同一帐户的其他设备上找不到它

This code add name to contact and on the phone I can see that on the phone it's on xxx@gmail.com account, but it's not sync with the remote account. I can't find it on gmail account as contact or on other device which same account

我也尝试静态输入谷歌账户 xxxx@gmail.com 但结果会一样,添加到手机联系人但不与谷歌账户同步.

I also try to input statically google account xxxx@gmail.com but result will be the same, add to phone contact but not synch with google account.

更新代码没问题,我忘了在我的设备上启用 google 帐户同步

UPDATE Code is OK, I forgot to enable synch of google account on my device

推荐答案

您的代码在我的设备(Android 4.0.4 和 4.1.2)上运行良好,在 Google 服务器上的帐户联系人自动显示,并从一台设备显示到另一台设备.顺便说一句,非常感谢您提供代码.

Your code works fine on my devices (Android 4.0.4 and 4.1.2), on Google server contacts for account appear automatically and from one device to other device. Thank you very much for the code, by the way.

恕我直言,问题不是代码,而是您设备的同步设置.

IMHO the issue is not the code, but sync settings of your device.