-
-
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); | |
} |
Fixed, thank you!
Instructions twice say to add method initializeAdapter, but this gist has initializeSyncAdapter, and the bottom of the instructions says to add SunshineSyncAdapter.initializeSyncAdapter(this); to the end of MainActivity. Assuming it's supposed to be initializeSyncAdapter, where exactly at "the end of MainActivity" is it supposed to be? Did you mean the end of the onCreate mthod? I'm pretty sure you can't just throw that in the end of the class declaration outside of a method....?
It is at the end of onCreate()
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
Error on line 12: "cannot resolve methode setExtras()".
removed .build() from line 11 to remove the bug.