Skip to content

Instantly share code, notes, and snippets.

@vmlinz
Created March 15, 2016 12:43
Show Gist options
  • Save vmlinz/865f622af2926e6f717e to your computer and use it in GitHub Desktop.
Save vmlinz/865f622af2926e6f717e to your computer and use it in GitHub Desktop.
Udacity "Developing Android Apps" #Sheduled Synchronization instructor notes

Instructor Notes

Before completing the quiz above, make sure you have added the necessary code to configure periodic syncs.

The code in this video will create the logic to, when you start Sunshine, check if an account has been created for Sunshine and if not, create a new account and configure a periodic sync to start.

Instructions

FetchWeatherTask and the SunshineService are now just older, unused classes which do the same thing that the SunshineSyncAdapter class is doing now; remove them. Clean up the AndroidManifest.xml and remove SunshineService and the AlarmReciever from the xml. In onPreferenceChange in the SettingsActivity, call syncImmediately() instead of creating and executing a FetchWeatherTask.

Add the following methods to SunshineSyncAdapter :

configurePeriodicSync onAccountCreated initializeAdapter SunshineSyncAdapter.java Gist for methods

At the top of SunshineSyncAdapter add the following constants for sync time. Phones capable of doing flexible syncs will sync more often.

// Interval at which to sync with the weather, in milliseconds. // 60 seconds (1 minute) * 180 = 3 hours public static final int SYNC_INTERVAL = 60 * 180; public static final int SYNC_FLEXTIME = SYNC_INTERVAL/3; Add the line onAccountCreated() to the method getSyncAccount after you find that a new account has been created. You can copy and replace using the method from the gist:

getSyncAccount Gist

Go to MainActivity. At the end of MainActivity call: SunshineSyncAdapter.initializeSyncAdapter(this);

The control flow goes:

Your MainActivity is created and the sync adapter is initialized. During initialization, getSyncAccount is called. getSyncAccount will create a new account if no sunshine.example.com account exists. If this is the case, onAccountCreated will be called. onAccountCreated configures the periodic sync and calls for an immediate sync. At this point, Sunshine will sync with the Open Weather API either every 3 hours (if the build version is less than KitKat) or everyone 1 hour (if the build version is greater than or equal to KitKat) If you'd like to quickly test that your periodic sync code is working:

Change the SYNC_INTERVAL time; set it to something low, like 30 seconds. Add a Log statement in onPerformSync Uninstall Sunshine or remove the Sunshine account. Finally run Sunshine again and confirm syncs are being called. You will want to undo these changes and uninstall Sunshine again when done so that your phone is not syncing very 30 seconds.

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