Android - sync contact add programmatically to google account(Android - 以编程方式同步联系人添加到谷歌帐户)
问题描述
在我的应用中,我需要将联系人添加到默认谷歌帐户并同步它.
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.
这篇关于Android - 以编程方式同步联系人添加到谷歌帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android - 以编程方式同步联系人添加到谷歌帐户
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01