首页 > 解决方案 > 有没有办法知道谁在调用同步适配器?

问题描述

我的项目中有内容提供者和同步适配器。在我的项目中,首先我将从服务器获取数据,然后将其存储在我的数据库 sqlite 中。

现在我来谈谈我的问题。其实我想根据sqlite中修改的数据来改变服务器数据。

例如:如果我删除数据库中的数据,我还想删除服务器数据库中的数据。为此,我的同步适配器应该知道执行了什么操作,例如删除或插入。

另一种方法是在调用同步适配器时将我的整个本地 sqlite 数据库与服务器中的数据进行比较。

我不想要那种方法。有什么方法可以知道谁在调用同步适配器或同步适配器在内容提供程序中执行了什么操作?

标签: javaandroidandroid-syncadapter

解决方案


术语“谁在调用同步适配器”似乎没有什么价值,只要它不会给你任何有用的信息。相反,您很可能希望在有人要求同步时传递一些参数(通过requestSync())。

ContentResolver.requestSync(Account, String, Bundle)Bundle接受将传递给您SyncAdapter的额外内容onPerformSync()



    // a code that initiates sync 
    ContentResolver.requestSync(ACCOUNT, AUTHORITY, someBundle);

    ...

    // your implmementation of SyncAdapter
    @Override
    public void onPerformSync(
            Account account,
            Bundle extras, // acquire information from this bundle
            String authority,
            ContentProviderClient provider,
            SyncResult syncResult) {
           /*
            * Put the data transfer code here.
            */
            ...
    } 


推荐阅读