Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Enable automatic sync when sync adapter is setup

I am writing an application which uses a sync adapter to synchronize data.

I've read up on the documentation and I'm pretty sure I understand how it all works. I've got most of my sync adapter working by following this great guide written by Udi Cohen.

However I do have one issue which I can't seem to solve, and thats enabling sync automatically when my app is installed.

When my app runs up, it creates a sync adapter and an account, does all the work you would expect it to do, which is great. However, if I go to Settings > Accounts > 'My App', the sync is off. Is there anyway I can get this to be automatically enabled?

Screenshot of Accounts > 'My App'

When setting up the sync adapter my code looks like this:

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 up around sync adapters, I believed that ContentResolver.setSyncAutomatically() was the right method to use to enable sync automatically.

I have also tried setting ContentResolver.setMasterSyncAutomatically(true); but that doesn't seem to have any effect.

I have the permission android.permission.WRITE_SYNC_SETTINGS declared in my AndroidManifest so that's not the problem. I also have the sync service and xml file in my project too. I have attached the code for these below.

Has anyone else had a similar issue? Could it be a permissions problem? If anyone has any suggestions I would be happy to try them. Thanks.

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"/>
like image 878
James Warner Avatar asked Apr 20 '16 08:04

James Warner


People also ask

How do I set up auto sync on Android?

Step 1: Tap the menu in the top left to see your preferences and other options. Step 2: Tap on the Manage Auto Sync text to open auto sync preferences on your device. Step 3: Tap the green Turn Auto Sync On button to enable auto sync.

Should I turn off auto sync Android?

If you leave auto-sync on, you may find your Android device running low on battery power much quicker than you want. So it's best to turn it off unless you really need it.

What is Android sync adapter?

The sync adapter component in your app encapsulates the code for the tasks that transfer data between the device and a server. Based on the scheduling and triggers you provide in your app, the sync adapter framework runs the code in the sync adapter component.

What is Auto sync?

With auto-sync, you no longer have to transfer data manually, saving you time and making sure that essential data is backed up to another device. The Gmail app syncs data automatically into data clouds so you can access information off of any device at any time.


1 Answers

I eventually found out what was causing my issue.

In my syncadapter.xml I had my content authority set to android:contentAuthority="com.android.calendar".

However, in my code where I was setting the sync to automatic using ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true); my content authority value was actually set to something different from my content authority in the XML.

The content authority when setting up your sync adapter must match the content authority used in the XML.

like image 55
James Warner Avatar answered Oct 13 '22 10:10

James Warner