Stop the synchronization adapter for synchronization when using addPeriodicSync

I am using a sync adapter in my project that will periodically sync. To create an account for the sync adapter, I use the code below.

The problem I am facing is that this code starts the initial synchronization. There is no mention in the documentation that this code will make the synchronization work first.

In fact, even in the Google sample project, there is additional code to start the initial synchronization, which I deleted.

I used the code from this example: http://developer.android.com/samples/BasicSyncAdapter/index.html

Even if I add the command ContentResolver.cancelSync (account, null); the sync adapter is still working.

How can I sync the sync adapter first. It must be synchronized for the first time when the period of the synchronization interval has passed.

Account account = new Account(context.getPackageName(), context.getPackageName()); AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); if (accountManager.addAccountExplicitly(account, null, null)) { // Inform the system that this account supports sync ContentResolver.setIsSyncable(account, context.getPackageName(), 1); // Inform the system that this account is eligible for auto sync when the network is up ContentResolver.setSyncAutomatically(account, context.getPackageName(), true); // Recommend a schedule for automatic synchronization. // The system may modify this based // on other scheduled syncs and network utilization. ContentResolver.addPeriodicSync(account, context.getPackageName(), Bundle.EMPTY, AppConstants.SYNC_INTERVAL); } 
+7
android android-contentresolver android-syncadapter
source share
2 answers

You can plan a future event after manual synchronization manually.

 private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); private void setDelayedAutoSync() { ScheduledFuture<?> countdown = scheduler.schedule(new Runnable() { @Override public void run() { Log.d(TAG, "Out of time!"); ContentResolver.setSyncAutomatically(account, content_authority, true); ContentResolver.addPeriodicSync(account, content_authority, new Bundle(),SYNC_FREQUENCY_CONSTANT); }, SYNC_FREQUENCY_CONSTANT, TimeUnit.SECONDS); } 
0
source share

The initial synchronization occurs as a result of adding the account explicitly.

  if (accountManager.addAccountExplicitly(account, null, null)) 

There is a broadcast sent by the synchronization adapter when there is an add / remove account that starts synchronization. Please refer to the original SyncManager class.

This can be avoided by adding a specific key to the Bundle passed to onPerformSync (), and make sure it triggers synchronization instead of sending an empty packet.

  Bundle bundle = new Bundle(); bundle.putBoolean("MySync", true); ContentResolver.addPeriodicSync(account, context.getPackageName(), bundle, AppConstants.SYNC_INTERVAL); .... onPerformSync(...) { if(bundle.containsKey("MySync")) { //perform your sync } } 
0
source share

All Articles