安卓同步适配器 - 手动同步

3

我已经花了几个小时尝试创建一个同步适配器。

同步设置总是处于未激活状态。为什么呢?

AndroidManifest

    <provider
        android:name="com.example.authenticating.DataProvider"
        android:authorities="@string/content_authority"
        android:exported="false"
        android:multiprocess="true"
        android:syncable="true" />

    <service
        android:name="com.example.authenticating.AuthenticatationService"
        android:exported="true"
        android:process=":auth">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>
        <meta-data
            android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" />
    </service>

syncadapter.xml

    <sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="@string/content_authority"
    android:accountType="@string/sync_account_type"
    android:userVisible="false"
    android:supportsUploading="false"
    android:allowParallelSyncs="false"
    android:isAlwaysSyncable="true" />

添加适配器

    Account account = new Account(getString(R.string.sync_account_name), getString(R.string.sync_sync_account_type_name));
    ContentResolver.setIsSyncable(account, getString(R.string.content_authority), 1);
    accountManager.addAccountExplicitly(account, "", null);

enter image description here

2个回答

1
如果您想执行手动同步,您需要调用以下方法:
Bundle settings = new Bundle();
settings.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
settings.putBooleanContentResolver.SYNC_EXTRAS_EXPEDITED, true);
ContentResolver.requestSync(mAccount, AUTHORITY, settingsBundle);

0
如果您想启用同步适配器,您需要将自动同步设置为true,即:
ContentResolver.setSyncAutomatically(mAccount, AUTHORITY, true);

这将在您的帐户下设置检查并启用同步适配器,使其在设备收到网络提醒时运行(请参阅此处)。 请确保将以下权限添加到您的清单文件中:

android.permission.WRITE_SYNC_SETTINGS

文档并没有明确说明这一点,但据我所知,即使您不将同步适配器作为“自动同步”启用,它仍将与您在其上设置的手动同步或定期同步一起工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接