Blindsurfer
Blindsurfer

Reputation: 423

Android Sync Adapter - manual Sync

i'm trying to create a Sync Adapter for hours now.

The Sync-Setting is always deactivated. Why?

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" />

Adding the Adapter

    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

Upvotes: 3

Views: 788

Answers (2)

TheSmile
TheSmile

Reputation: 39

If you want to enable sync adapters, you need to set auto-sync to true, which would be:

ContentResolver.setSyncAutomatically(mAccount, AUTHORITY, true);

That would set the check under your account and enable the sync adapter to run whenever the device gets a network tickle (see here) Make sure to add the following permission to you manifest:

android.permission.WRITE_SYNC_SETTINGS

The documentation does not explicitly say anything about it, but as far as I know, even if you don't "enable" the sync adapter as "auto-sync" it will still work with manual syncing or periodical sync that you set on it.

Upvotes: 0

avmatte
avmatte

Reputation: 631

If you want to perform a manual sync you have to call:

Bundle settings = new Bundle();
settings.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
settings.putBooleanContentResolver.SYNC_EXTRAS_EXPEDITED, true);
ContentResolver.requestSync(mAccount, AUTHORITY, settingsBundle);

Upvotes: 1

Related Questions