For the More Curious: Sync Adapters

Yet another way to set up a regularly polling web service is to use a sync adapter. Sync adapters are not adapters like you have seen before. Instead, their sole purpose is to sync data with a data source (uploading, downloading, or both). Unlike JobScheduler, sync adapters have been around for a while, so you do not have to worry about which version of Android you are running.

Like JobScheduler, sync adapters can be used as a replacement for the AlarmManager setup that you have in PhotoGallery. Syncs from multiple applications are grouped together by default, without you having to set flags a certain way. Furthermore, you do not have to worry about resetting the sync alarm across reboots because sync adapters handle this for you.

Sync adapters also integrate nicely with the OS from a user perspective. You can expose your app as a sync-able account that the user can manage through the SettingsAccounts menu. This is where users manage accounts for other apps that use sync adapters, such as Google’s suite of apps (Figure 28.2).

Figure 28.2  Accounts settings

Set of three screenshots are shown.

While using a sync adapter makes correct usage of scheduling repeating network work easier – and allows you to get rid of the alarm management and pending intent code – a sync adapter does require a bunch more code. First, a sync adapter does not do any of your web requests for you, so you still have to write that code (e.g., FlickrFetchr). Second, it requires a content provider implementation to wrap the data, account, and authenticator classes to represent an account on a remote server (even if the server does not require authentication), as well as a sync adapter and sync service implementation. It also requires working knowledge of bound services.

So if your application already uses a ContentProvider for its data layer and requires account authentication, using a sync adapter is a good option for you to consider. It is a big advantage that sync adapters integrate with the UI provided by the OS, too. JobScheduler does not do that. If none of those considerations apply, the extra code required might not be worth it.

The online developer docs provide a tutorial on using sync adapters at developer.android.com/​training/​sync-adapters/​index.xhtml. Check it out to learn more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.202.187