Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active April 16, 2017 20:20
Show Gist options
  • Save udacityandroid/7230489fb8cb3f46afee to your computer and use it in GitHub Desktop.
Save udacityandroid/7230489fb8cb3f46afee to your computer and use it in GitHub Desktop.
Methods for periodic syncing in the SunshineSyncAdapter
/**
* Helper method to schedule the sync adapter periodic execution
*/
public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
Account account = getSyncAccount(context);
String authority = context.getString(R.string.content_authority);
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);
}
}
private static void onAccountCreated(Account newAccount, Context context) {
/*
* Since we've created an account
*/
SunshineSyncAdapter.configurePeriodicSync(context, SYNC_INTERVAL, SYNC_FLEXTIME);
/*
* Without calling setSyncAutomatically, our periodic sync will not be enabled.
*/
ContentResolver.setSyncAutomatically(newAccount, context.getString(R.string.content_authority), true);
/*
* Finally, let's do a sync to get things started
*/
syncImmediately(context);
}
public static void initializeSyncAdapter(Context context) {
getSyncAccount(context);
}
@tarunkukreja003
Copy link

It is at the end of onCreate()

@francisrod01
Copy link

There's a problem in configurePeriodicSync() method to invokate SyncRequest() that is not public in android.content.SyncRequest().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment