WARNING: Many of the APIs used in this code have been deprecated since I initially wrote this post.
Check out the official documentation for the latest instructions.
One of the trickiest aspects of writing a robust web-based Android application
is authentication, simply due to its asynchronous nature and the many edge cases
that one must cover. Thankfully, the recently released Google Play Services API
greatly simplifies the authentication process, providing developers with a
consistent and safe way to grant and receive OAuth2 access tokens to Google
services. Even so, there are still several cases that must be covered in order
to provide the best possible user experience. A professionally built Android
application should be able to react to even the most unlikely events, for example,
if a previously logged in user uninstalls Google Play Services, or navigates to
the system settings and clears the application’s data when the foreground Activity
is in a paused state. This post focuses on how to make use of the Google Play
Services library while still accounting for edge cases such as these.