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