Android-同步联系人以编程方式添加到Google帐户 [英] Android - sync contact add programmatically to google account

查看:312
本文介绍了Android-同步联系人以编程方式添加到Google帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我需要将联系人添加到默认的Google帐户并进行同步。

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

我也尝试静态输入Google帐户xxxx@gmail.com,但结果将是同样,将其添加到电话联系人中,但不与Google帐户同步。

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.

这篇关于Android-同步联系人以编程方式添加到Google帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆