-
-
Save udacityandroid/7230489fb8cb3f46afee to your computer and use it in GitHub Desktop.
Methods for periodic syncing in the SunshineSyncAdapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} |
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
It is at the end of onCreate()