background preloader

Account & sync

Facebook Twitter

Android: sync adapter lifecycle. Recap: show the sync pending state in the UI and if you really have to know when the sync has ended, do not trust the ContentResolver sync methods. Hottest 'android-syncadapter' Answers. OnPerformSync | udinic. On my last post on the subject, Write your own Android Authenticator, we embarked on a journey to the unfamiliar part of authentication on Android. I received many positive responses about it from you all, and it seems I really helped a lot of people to get to know this feature well. Since then, I had a lot of stuff going on in my life (getting married is one of them) which delayed the release of this obvious sequel. On this post, our journey continues to another not-well-documented area, which goes hand-in-hand with our own authenticator.

It’s no other than the notorious SyncAdapter. I’ll show you how to make your app more efficient and robust when it comes to data synchronization, by using sync adapters. The web doesn’t have all the information I’d hope to find about it. I felt like I need to give this feature another one of my in-depth researches, see what this feature is all about, how it works, how to write one, and finally – report back. Terra Incognita So what is this SyncAdapter? Storage Options. Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires. Your data storage options are the following: Shared Preferences Store private primitive data in key-value pairs.

Internal Storage Store private data on the device memory. External Storage Store public data on the shared external storage. SQLite Databases Store structured data in a private database. Network Connection Store data on the web with your own network server. Android provides a way for you to expose even your private data to other applications — with a content provider. Using Shared Preferences The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. User Preferences To write values: // Commit the edits! For example: Transferring Data Using Sync Adapters. Synchronizing data between an Android device and web servers can make your application significantly more useful and compelling for your users.

For example, transferring data to a web server makes a useful backup, and transferring data from a server makes it available to the user even when the device is offline. In some cases, users may find it easier to enter and edit their data in a web interface and then have that data available on their device, or they may want to collect data over time and then upload it to a central storage area. Although you can design your own system for doing data transfers in your app, you should consider using Android's sync adapter framework. This framework helps manage and automate data transfers, and coordinates synchronization operations across different apps. When you use this framework, you can take advantage of several features that aren't available to data transfer schemes you design yourself: Plug-in architecture Automated execution Lessons.

Remembering Your User. Google Play services offers a standard authorization flow for all Google APIs and all components of Google Play services. In addition, you can leverage the authorization portion of the Google Play services SDK to gain authorization to services that are not yet supported in the Google Play services platform by using the access token to manually make API requests or using a client library provided by the service provider.

For implementation details, see the sample in <android-sdk>/extras/google-play-services/samples/auth, which shows you how to carry out these basic steps for obtaining an access token. Choosing an Account Google Play services leverage existing accounts on an Android-powered device to gain authorization to the services that you want to use. For example, here's how to gather all of the Google accounts on a device and return them in an array. Obtaining an Access Token With an email address and the service scope you can now obtain an access token. Using getToken() Creating a Custom Account Type.