Android Enable auto-sync when configuring a sync adapter.

I am writing an application that uses a synchronization adapter to synchronize data.

I read the documentation and I'm sure I understand how it all works. I have most of my sync adapter working, following this excellent tutorial written by Udi Cohen.

However, I have one problem that I cannot solve, and this allows me to automatically synchronize when my application is installed.

When my application starts, it creates a sync adapter and an account, does all the work that you expect from it, and that's great. However, if I go to Settings> Accounts> My Application, sync is disabled. Anyway, can I get this to automatically activate?

Account Screenshot> My Application

When setting up the sync adapter, my code is as follows:

if(accountManager.addAccountExplicitly(account,null,null))
{
    // Inform the system that this account supports sync
    ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);

    // Inform the system that this account is eligible for auto sync
    ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);

    // Recommend a schedule for auto sync
    ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, new Bundle(), SYNC_FREQUENCY);

    newAccount = true;
}

From reading synchronization adapters, I thought I ContentResolver.setSyncAutomatically()was the right method for automatic synchronization.

I also tried to install ContentResolver.setMasterSyncAutomatically(true);, but this has no effect.

android.permission.WRITE_SYNC_SETTINGS, AndroidManifest, . xml . .

- ? ? - , . .

AndroidManifest.xml

<service
    android:name=".syncadapter.CalendarSyncService"
    android:exported="true"
    android:process=":sync">
    <intent-filter>
        <action android:name="android.content.SyncAdapter"/>
    </intent-filter>
    <meta-data
        android:name="android.content.SyncAdapter"
        android:resource="@xml/calendar_syncadapter"/>
</service>

syncadapter.xml

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
              android:accountType="com.companyname.rostering"
              android:allowParallelSyncs="true"
              android:contentAuthority="com.android.calendar"
              android:isAlwaysSyncable="true"
              android:supportsUploading="true"
              android:userVisible="true"/>
+4
2

, .

syncadapter.xml android:contentAuthority="com.android.calendar".

, ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);, -, XML.

, XML.

+3

, :

     <service
        android:name=".SyncService"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="android.content.SyncAdapter" />
        </intent-filter>
        <meta-data
            android:name="android.content.SyncAdapter"
            android:resource="@xml/syncadapter" />
    </service>

syncadapter.xml :

<?xml version="1.0" encoding="utf-8"?>

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

android:isAlwaysSyncable false , , ,

UPDATE

, , KITKAT, , , addPeriodicSync:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // we can enable inexact timers in our periodic sync
        SyncRequest request = new SyncRequest.Builder().
                syncPeriodic(syncInterval, flexTime).
                setSyncAdapter(account, authority).
                setExtras(new Bundle()).build();
        ContentResolver.requestSync(request);
    } else {
        ContentResolver.addPeriodicSync(account,
                authority, new Bundle(), syncInterval);
0

All Articles