|
| 1 | +--- |
| 2 | +title: Android |
| 3 | +--- |
| 4 | + |
| 5 | +# Running on [Android (@Beta)](#@Beta) |
| 6 | + |
| 7 | +If you are developing for Android and the Google API you want to use is included |
| 8 | +in the [Google Play Services library][play-services], use that library for the |
| 9 | +best performance and experience. |
| 10 | + |
| 11 | +To access other Google APIs, use the Google Client Library for Java's |
| 12 | +Android-specific helper classes, which are well-integrated with |
| 13 | +[Android AccountManager][account-manager]. |
| 14 | + |
| 15 | +For example: |
| 16 | + |
| 17 | +```java |
| 18 | +@Override |
| 19 | +public void onCreate(Bundle savedInstanceState) { |
| 20 | + super.onCreate(savedInstanceState); |
| 21 | + // Google Accounts |
| 22 | + credential = |
| 23 | + GoogleAccountCredential.usingOAuth2(this, Collections.singleton(TasksScopes.TASKS)); |
| 24 | + SharedPreferences settings = getPreferences(Context.MODE_PRIVATE); |
| 25 | + credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null)); |
| 26 | + // Tasks client |
| 27 | + service = |
| 28 | + new com.google.api.services.tasks.Tasks.Builder(httpTransport, jsonFactory, credential) |
| 29 | + .setApplicationName("Google-TasksAndroidSample/1.0").build(); |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Getting started |
| 34 | + |
| 35 | +Begin by reading the [Android development instructions][http-client-android] for |
| 36 | +the Google HTTP Client Library for Java. |
| 37 | + |
| 38 | +## Authentication |
| 39 | + |
| 40 | +As described in the [Android development instructions][http-client-android], the |
| 41 | +best practice on Android is to use the [`AccountManager`][account-manager] class |
| 42 | +(`@Beta`) for centralized identity management and credential token storage. |
| 43 | + |
| 44 | +For information about the OAuth 2.0 flow, see the |
| 45 | +[OAuth 2.0 instructions for Android][oauth2-android]. |
| 46 | + |
| 47 | +## Partial response and update |
| 48 | + |
| 49 | +Google APIs support a partial-response protocol that allows you to specify which |
| 50 | +fields are returned to you in the HTTP response. This can significantly reduce |
| 51 | +the size of the response, thereby reducing network usage, parsing response time, |
| 52 | +and memory usage. It works with both JSON and XML. |
| 53 | + |
| 54 | +The following snippet of code drawn from the Google+ Sample demonstrates how to |
| 55 | +use the partial-response protocol: |
| 56 | + |
| 57 | + |
| 58 | +```java |
| 59 | +Plus.Activities.List listActivities = plus.activities().list("me", "public"); |
| 60 | +listActivities.setMaxResults(5L); |
| 61 | +// Pro tip: Use partial responses to improve response time considerably |
| 62 | +listActivities.setFields("nextPageToken,items(id,URL,object/content)"); |
| 63 | +ActivityFeed feed = listActivities.execute(); |
| 64 | +``` |
| 65 | + |
| 66 | +[play-services]: https://developer.android.com/google/play-services/index.html |
| 67 | +[account-manager]: http://developer.android.com/reference/android/accounts/AccountManager.html |
| 68 | +[http-client-android]: https://github.com/googleapis/google-http-java-client/wiki/Android |
| 69 | +[oauth2-android]: https://github.com/googleapis/google-api-java-client#oauth2-android |
0 commit comments