使用SyncAdapter同步联系人,正在工作 [英] Syncing contacts using SyncAdapter ,Working

本文介绍了使用SyncAdapter同步联系人,正在工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在一个Android项目中,我想将移动联系人同步到服务器,在对 SyncAdapter 进行了大量研究并在 accountmanager 中创建帐户之后,我有了我自己学的.但是,有两件事我不理解,我在google中搜索了这些内容,但找不到完美的答案.请不要重复这个问题,我想更清楚地知道它是如何工作的.

I am currently working on a android project in which I want to Sync mobile contacts to server, After researching a lot about SyncAdapter and Creating account in accountmanager, I have learned it myself. however there are two things I don't understand, I searched about these in google but could not get perfect answer. Please don't duplicate the question, I want to know more clearly how it works.

  1. 正如Google文档所述,每当同步完成时,联系人的肮脏标志都会更改.我的疑问是,当我以编程方式指定将哪些联系人发送到服务器时,android操作系统将如何检测该特定联系人的同步状态?

Ex:-在下面的示例中,我想将捆绑包发送到服务器.

Ex:- In below example, I want to send a bundle to server.

    Bundle extras = new Bundle();
      extras.putInt("contact name after filtering", "number after filtering");  
      ContentResolver.requestSync(account,ContactsContract.AUTHORITY, extras);

然后在 onPerformSync 方法上收到的捆绑包附加内容并发送到服务器.

then the Bundle extras received on the onPerformSync Method and send to server.

    @Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {

    for (String key : extras.keySet())
    {
        Log.d("Bundle Debug", key + " = \"" + extras.get(key) + "\"");
    }

       //Code to send to server

}

假设在上面的示例中,变量Extras是一个捆绑包,它是在过滤联系人列表中的更改后获得的,想要将其更新到服务器.现在,SyncAdapter框架或OS将如何检测到更改并更新该联系人的脏标志?当然,我已经提供了 AUTHORITY ,不知道这对查找更改有何帮助.以上方法是正确的实现方法吗?

Let's say in the above example, variable extras is a bundle which obtained after filtering changes in the contacts list,want to update them to server.Now how will the SyncAdapter framework or OS detect the changes and update dirty flag of that contact? Of course I have provided AUTHORITY, don't know how does that help in finding what changed. IS the above method is correct way to implement?

  1. 如何查找同步是否失败?或完成?

推荐答案

在没有 DELETED 标志,而不是删除联系人.

Every modification of a contact that's done without the CALLER_IS_SYNC_ADAPTER on the Uri will make the ContentProvider set the DIRTY flag of the modified contact to 1. Similarly, every delete request without that parameter will just set the DELETED flag instead of deleting the contact.

您的SyncAdapter必须查询标记为脏或的联系人 DELETED ,执行适当的操作(将新的联系人数据发送到服务器或从服务器中删除联系人)并清除脏标志(通过将其替换为 0 CALLER_IS_SYNCADAPTER 参数的代码>,或者通过再次删除联系人来完成删除操作(再次设置了 CALLER_IS_SYNCADAPTER 参数).

Your SyncAdapter has to query the contacts that are flagged dirty or DELETED, take the appropriate action (send the new contact data to the server or delete the contacts from the server) and clear the dirty flag (by overriding it with 0 having the CALLER_IS_SYNCADAPTER parameter in place) or finish the removal by deleting the contact again (again having the CALLER_IS_SYNCADAPTER parameter in place).

我认为您不能通过将 DELETED 设置为 0 来取消删除"联系人,因为(根据我的经验)该联系人数据已被删除..仅保留了RawContact条目(不过,也许上次尝试该设备时行为异常).

I believe that you can not "undelete" a contact by setting DELETED to 0, since (to my experience) the contact data has already been removed at that point. Only the RawContact entry is left (though, maybe I had just a misbehaving device when I tried that the last time).

指定 CALLER_IS_SYNCADAPTER 很重要,否则将不会发生任何事情(并且您的SyncAdapter注定要尝试一次又一次地同步这些联系人).

It's important to specify the CALLER_IS_SYNCADAPTER, otherwise nothing will happen (and your SyncAdapter is doomed to try to sync these contacts again and again).

关于问题2:

这完全取决于您的SyncAdapter.您编写了同步联系人的代码,并且您是SyncAdapter唯一知道它是否成功的人.通常,如果在同步过程中未引发任何异常,则可以假定它已成功.

That's completely up to your SyncAdapter. You write the code to sync the contacts and you're SyncAdapter is the only one to tell if it succeeded or not. In general you probably can assume it succeeded if no Exceptions were thrown during the sync.

这篇关于使用SyncAdapter同步联系人,正在工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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