summaryrefslogtreecommitdiffstats
path: root/docs/html/google/auth/api-client.jd
blob: e33721dcffc0880791c39599d567d2348cbd9525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
page.title=Accessing Google APIs
page.tags="oauth 2.0","GoogleAuthUtil"

trainingnavtop=true
startpage=true

@jd:body

<div id="qv-wrapper">
  <div id="qv">

<h2>In this document</h2>
<ol>
  <li><a href="#Starting">Start a Connection</a>
    <ol>
      <li><a href="#HandlingFailures">Handle connection failures</a></li>
      <li><a href="#MaintainingState">Maintain state while resolving an error</a></li>
      <li><a href="#WearableApi">Access the Wearable API</a></li>
    </ol>
  </li>
  <li><a href="#Communicating">Communicate with Google Services</a>
    <ol>
      <li><a href="#Async">Using asynchronous calls</a></li>
      <li><a href="#Sync">Using synchronous calls</a></li>
    </ol>
  </li>
</ol>
</div>
</div>


<p>When you want to make a connection to one of the Google APIs provided in the Google Play services
library (such as Google+, Games, or Drive), you need to create an instance of <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a> ("Google API Client"). The Google API Client provides a common entry point to all
the Google Play services and manages the network connection between the user's device and each
Google service.</p>

<div class="sidebox" style="clear:right;width:190px">
<h2>Connecting to REST APIs</h2>
<p>If the Google API you want to use is not included in the Google Play services library, you can
connect using the appropriate REST API, but you must obtain an OAuth 2.0 token. For more
information, read <a href="{@docRoot}google/auth/http-auth.html">Authorizing with Google
for REST APIs</a>.</p>
</div>

<p>This guide shows how you can use Google API Client to:</p>
<ul>
<li>Connect to one or more Google Play services asynchronously and handle failures.</li>
<li>Perform synchronous and asynchronous API calls to any of the Google Play services.</li>
</ul>

<p class="note">
<strong>Note:</strong> If you have an existing app that connects to Google Play services with a
subclass of {@code GooglePlayServicesClient}, you should migrate to <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a> as soon as possible.</p>


<img src="{@docRoot}images/google/GoogleApiClient@2x.png" width="464px" alt="" />
<p class="img-caption">
<strong>Figure 1.</strong> An illustration showing how the Google API Client provides an
interface for connecting and making calls to any of the available Google Play services such as
Google Play Games and Google Drive.</p>



<p>To get started, you must first install the Google Play services library (revision 15 or higher) for
your Android SDK. If you haven't done so already, follow the instructions in <a
href="{@docRoot}google/play-services/setup.html">Set Up Google
Play Services SDK</a>.</p>




<h2 id="Starting">Start a Connection</h2>

<p>Once your project is linked to the Google Play services library, create an instance of <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a> using the <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.Builder.html">{@code
GoogleApiClient.Builder}</a> APIs in your activity's {@link
android.app.Activity#onCreate onCreate()} method. The <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.Builder.html">{@code
GoogleApiClient.Builder}</a> class
provides methods that allow you to specify the Google APIs you want to use and your desired OAuth
2.0 scopes. For example, here's a <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a> instance that connects with the Google
Drive service:</p>
<pre>
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .build();
</pre>

<p>You can add multiple APIs and multiple scopes to the same <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a> by appending
additional calls to
<a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.Builder.html#addApi(com.google.android.gms.common.api.Api)"
>{@code addApi()}</a> and
<a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.Builder.html#addScope(com.google.android.gms.common.api.Scope)"
>{@code addScope()}</a>.</p>

<p class="caution">
<strong>Important:</strong> To avoid client connection errors on devices that do not have the
<a href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en">Android
Wear app</a> installed, use a separate <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a> instance to access only the <a
href="{@docRoot}reference/com/google/android/gms/wearable/Wearable.html">{@code
Wearable}</a> API. For more information, see <a href="#WearableApi">Access the Wearable
API</a>.</p>

<p>Before you can begin a connection by calling <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#connect()"
>{@code connect()}</a> on the <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a>, you must specify an implementation for the callback interfaces, <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.ConnectionCallbacks.html"
>{@code ConnectionCallbacks}</a> and <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.OnConnectionFailedListener.html"
>{@code OnConnectionFailedListener}</a>. These interfaces receive callbacks in
response to the asynchronous <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#connect()"
>{@code connect()}</a> method when the connection to Google Play services
succeeds, fails, or becomes suspended.</p>

<p>For example, here's an activity that implements the callback interfaces and adds them to the Google
API Client:</p>

<pre>
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import gms.drive.*;
import android.support.v4.app.FragmentActivity;

public class MyActivity extends FragmentActivity
        implements ConnectionCallbacks, OnConnectionFailedListener {
    private GoogleApiClient mGoogleApiClient;

    &#64;Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create a GoogleApiClient instance
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        ...
    }

    &#64;Override
    public void onConnected(Bundle connectionHint) {
        // Connected to Google Play services!
        // The good stuff goes here.
    }

    &#64;Override
    public void onConnectionSuspended(int cause) {
        // The connection has been interrupted.
        // Disable any UI components that depend on Google APIs
        // until onConnected() is called.
    }

    &#64;Override
    public void onConnectionFailed(ConnectionResult result) {
        // This callback is important for handling errors that
        // may occur while attempting to connect with Google.
        //
        // More about this in the next section.
        ...
    }
}
</pre>

<p>With the callback interfaces defined, you're ready to call <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#connect()"
>{@code connect()}</a>. To gracefully manage
the lifecycle of the connection, you should call <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#connect()"
>{@code connect()}</a> during the activity's {@link
android.app.Activity#onStart onStart()} (unless you want to connect later), then call <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#disconnect()"
>{@code disconnect()}</a> during the {@link android.app.Activity#onStop onStop()} method. For example:</p>
<pre>
    &#64;Override
    protected void onStart() {
        super.onStart();
        if (!mResolvingError) {  // more about this later
            mGoogleApiClient.connect();
        }
    }

    &#64;Override
    protected void onStop() {
        mGoogleApiClient.disconnect();
        super.onStop();
    }
</pre>

<p>However, if you run this code, there's a good chance it will fail and your app will receive a call
to <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.OnConnectionFailedListener.html#onConnectionFailed(com.google.android.gms.common.ConnectionResult)"
>{@code onConnectionFailed()}</a> with the <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SIGN_IN_REQUIRED"
>{@code SIGN_IN_REQUIRED}</a> error because the user account
has not been specified. The next section shows how to handle this error and others.</p>


<h3 id="HandlingFailures">Handle connection failures</h3>

<p>When you receive a call to the
<a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.OnConnectionFailedListener.html#onConnectionFailed(com.google.android.gms.common.ConnectionResult)">
{@code onConnectionFailed()}</a> callback, you should call <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#hasResolution()"
>{@code hasResolution()}</a> on the provided <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html"
>{@code ConnectionResult}</a> object. If it returns true, you can
request the user take immediate action to resolve the error by calling <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity, int)">{@code startResolutionForResult()}</a> on the <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html"
>{@code ConnectionResult}</a> object. The <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity, int)"
>{@code startResolutionForResult()}</a> behaves the same as {@link
android.app.Activity#startActivityForResult startActivityForResult()} and launches the
appropriate activity for the user
to resolve the error (such as an activity to select an account).</p>

<p>If <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#hasResolution()"
>{@code hasResolution()}</a> returns false, you should instead call <a
href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#getErrorDialog(int, android.app.Activity, int)"
>{@code GooglePlayServicesUtil.getErrorDialog()}</a>, passing it the error code. This returns a {@link
android.app.Dialog} provided by Google Play services that's appropriate for the given error. The
dialog may simply provide a message explaining the error, but it may also provide an action to
launch an activity that can resolve the error (such as when the user needs to install a newer
version of Google Play services).</p>

<p>For example, your
<a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.OnConnectionFailedListener.html#onConnectionFailed(com.google.android.gms.common.ConnectionResult)"
>{@code onConnectionFailed()}</a> callback method should now look like this:</p>

<pre>
public class MyActivity extends FragmentActivity
        implements ConnectionCallbacks, OnConnectionFailedListener {

    // Request code to use when launching the resolution activity
    private static final int REQUEST_RESOLVE_ERROR = 1001;
    // Unique tag for the error dialog fragment
    private static final String DIALOG_ERROR = "dialog_error";
    // Bool to track whether the app is already resolving an error
    private boolean mResolvingError = false;

    ...

    &#64;Override
    public void onConnectionFailed(ConnectionResult result) {
        if (mResolvingError) {
            // Already attempting to resolve an error.
            return;
        } else if (result.hasResolution()) {
            try {
                mResolvingError = true;
                result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
            } catch (SendIntentException e) {
                // There was an error with the resolution intent. Try again.
                mGoogleApiClient.connect();
            }
        } else {
            // Show dialog using GooglePlayServicesUtil.getErrorDialog()
            showErrorDialog(result.getErrorCode());
            mResolvingError = true;
        }
    }

    // The rest of this code is all about building the error dialog

    /* Creates a dialog for an error message */
    private void showErrorDialog(int errorCode) {
        // Create a fragment for the error dialog
        ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
        // Pass the error that should be displayed
        Bundle args = new Bundle();
        args.putInt(DIALOG_ERROR, errorCode);
        dialogFragment.setArguments(args);
        dialogFragment.show(getSupportFragmentManager(), "errordialog");
    }

    /* Called from ErrorDialogFragment when the dialog is dismissed. */
    public void onDialogDismissed() {
        mResolvingError = false;
    }

    /* A fragment to display an error dialog */
    public static class ErrorDialogFragment extends DialogFragment {
        public ErrorDialogFragment() { }

        &#64;Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Get the error code and retrieve the appropriate dialog
            int errorCode = this.getArguments().getInt(DIALOG_ERROR);
            return GooglePlayServicesUtil.getErrorDialog(errorCode,
                    this.getActivity(), REQUEST_RESOLVE_ERROR);
        }

        &#64;Override
        public void onDismiss(DialogInterface dialog) {
            ((MainActivity)getActivity()).onDialogDismissed();
        }
    }
}
</pre>

<p>Once the user completes the resolution provided by <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity, int)"
>{@code startResolutionForResult()}</a> or <a
href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#getErrorDialog(int, android.app.Activity, int)"
>{@code GooglePlayServicesUtil.getErrorDialog()}</a>, your activity receives the {@link
android.app.Activity#onActivityResult onActivityResult()} callback with the {@link
android.app.Activity#RESULT_OK}
result code. You can then call <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#connect()"
>{@code connect()}</a> again. For example:</p>

<pre>
&#64;Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_RESOLVE_ERROR) {
        mResolvingError = false;
        if (resultCode == RESULT_OK) {
            // Make sure the app is not already connected or attempting to connect
            if (!mGoogleApiClient.isConnecting() &&
                    !mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
            }
        }
    }
}
</pre>

<p>In the above code, you probably noticed the boolean, {@code mResolvingError}. This keeps track of
the app state while the user is resolving the error to avoid repetitive attempts to resolve the
same error. For instance, while the account picker dialog is showing to resolve the <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#SIGN_IN_REQUIRED"
>{@code SIGN_IN_REQUIRED}</a> error, the user may rotate the screen. This recreates your activity and causes
your {@link android.app.Activity#onStart onStart()} method to be called again, which then calls <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#connect()"
>{@code connect()}</a> again. This results in another call to <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity, int)"
>{@code startResolutionForResult()}</a>, which
creates another account picker dialog in front of the existing one.</p>

<p>This boolean is effective only
if retained across activity instances, though. The next section explains further.</p>



<h3 id="MaintainingState">Maintain state while resolving an error</h3>

<p>To avoid executing the code in
<a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.OnConnectionFailedListener.html#onConnectionFailed(com.google.android.gms.common.ConnectionResult)">
{@code onConnectionFailed()}</a> while a previous attempt to resolve an
error is ongoing, you need to retain a boolean that tracks whether your app is already attempting
to resolve an error.</p>

<p>As shown in the code above, you should set a boolean to {@code true} each time you call <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity, int)"
>{@code startResolutionForResult()}</a> or display the dialog from <a
href="{@docRoot}reference/com/google/android/gms/common/GooglePlayServicesUtil.html#getErrorDialog(int, android.app.Activity, int)"
>{@code GooglePlayServicesUtil.getErrorDialog()}</a>. Then when you
receive {@link android.app.Activity#RESULT_OK} in the {@link android.app.Activity#onActivityResult
onActivityResult()} callback, set the boolean to {@code false}.</p>

<p>To keep track of the boolean across activity restarts (such as when the user rotates the screen),
save the boolean in the activity's saved instance data using {@link
android.app.Activity#onSaveInstanceState onSaveInstanceState()}:</p>

<pre>
private static final String STATE_RESOLVING_ERROR = "resolving_error";

&#64;Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean(STATE_RESOLVING_ERROR, mResolvingError);
}
</pre>

<p>Then recover the saved state during {@link android.app.Activity#onCreate onCreate()}:</p>

<pre>
&#64;Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...
    mResolvingError = savedInstanceState != null
            && savedInstanceState.getBoolean(STATE_RESOLVING_ERROR, false);
}
</pre>

<p>Now you're ready to safely run your app and connect to Google Play services.
How you can perform read and write requests to any of the Google Play services
using <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a> is discussed in the next section.</p>

<p>For more information about each services's APIs available once you're connected,
consult the corresponding documentation, such as for
<a href="{@docRoot}google/play-services/games.html">Google Play Games</a> or
<a href="{@docRoot}google/play-services/drive.html">Google Drive</a>.
</p>


<h3 id="WearableApi">Access the Wearable API</h3>

<p>On devices that do not have the <a
href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en">Android
Wear app</a> installed, connection requests that include the <a
href="{@docRoot}reference/com/google/android/gms/wearable/Wearable.html">{@code
Wearable}</a> API fail with the <a
href="{@docRoot}reference/com/google/android/gms/common/ConnectionResult.html#API_UNAVAILABLE">
<code>API_UNAVAILABLE</code></a> error code. If your app uses the <a
href="{@docRoot}reference/com/google/android/gms/wearable/Wearable.html">{@code
Wearable}</a> API in addition to other Google APIs, use a separate <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a> instance to access the <a
href="{@docRoot}reference/com/google/android/gms/wearable/Wearable.html">{@code
Wearable}</a> API. This approach enables you to access other Google APIs on devices that are not
paired with a wearable device.</p>

<p>When you use a separate <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a> instance to access only the Wearable API, you can determine
whether the <a
href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en">Android
Wear app</a> is installed on the device:</p>

<pre>
// Connection failed listener method for a client that only
// requests access to the Wearable API
&#64;Override
public void onConnectionFailed(ConnectionResult result) {
    if (result.getErrorCode() == ConnectionResult.API_UNAVAILABLE) {
        // The Android Wear app is not installed
    }
    ...
}
</pre>






<h2 id="Communicating">Communicate with Google Services</h2>

<p>Once connected, your client can make read and write calls using the service-specific APIs for which
your app is authorized, as specified by the APIs and scopes you added to your <a
href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code
GoogleApiClient}</a> instance.</p>

<p class="note">
<strong>Note:</strong> Before making calls to specific Google services, you may first need to
register your app in the Google Developer Console. For specific instructions, refer to the
appropriate getting started guide for the API you're using, such as <a href=
"https://developers.google.com/drive/android/get-started">Google Drive</a> or <a href=
"https://developers.google.com/+/mobile/android/getting-started">Google+</a>.</p>

<p>When you perform a read or write request using Google API Client, the immediate result is returned
as a <a href="{@docRoot}reference/com/google/android/gms/common/api/PendingResult.html">{@code
PendingResult}</a> object. This is an object representing the request, which hasn't yet
been delivered to the Google service.</p>

<p>For example, here's a request to read a file from Google Drive that provides a
<a href="{@docRoot}reference/com/google/android/gms/common/api/PendingResult.html">{@code
PendingResult}</a> object:</p>

<pre>
Query query = new Query.Builder()
        .addFilter(Filters.eq(SearchableField.TITLE, filename));
PendingResult result = Drive.DriveApi.query(mGoogleApiClient, query);
</pre>

<p>Once you have the
<a href="{@docRoot}reference/com/google/android/gms/common/api/PendingResult.html">{@code
PendingResult}</a>, you can continue by making the request either asynchronous
or synchronous.</p>


<h3 id="Async">Using asynchronous calls</h3>

<p>To make the request asynchronous, call <a
href="{@docRoot}reference/com/google/android/gms/common/api/PendingResult.html#setResultCallback(com.google.android.gms.common.api.ResultCallback<R>)"
>{@code setResultCallback()}</a> on the
<a href="{@docRoot}reference/com/google/android/gms/common/api/PendingResult.html">{@code
PendingResult}</a> and
provide an implementation of the <a
href="{@docRoot}reference/com/google/android/gms/common/api/ResultCallback.html"
>{@code ResultCallback}</a> interface. For example, here's the request
executed asynchronously:</p>

<pre>
private void loadFile(String filename) {
    // Create a query for a specific filename in Drive.
    Query query = new Query.Builder()
            .addFilter(Filters.eq(SearchableField.TITLE, filename))
            .build();
    // Invoke the query asynchronously with a callback method
    Drive.DriveApi.query(mGoogleApiClient, query)
            .setResultCallback(new ResultCallback&lt;DriveApi.MetadataBufferResult>() {
        &#64;Override
        public void onResult(DriveApi.MetadataBufferResult result) {
            // Success! Handle the query result.
            ...
        }
    });
}
</pre>

<p>When your app receives a <a
href="{@docRoot}reference/com/google/android/gms/common/api/Result.html">{@code Result}</a>
object in the <a
href="{@docRoot}reference/com/google/android/gms/common/api/ResultCallback.html#onResult(R)"
>{@code onResult()}</a> callback, it is delivered as an instance of the
appropriate subclass as specified by the API you're using, such as <a
href="{@docRoot}reference/com/google/android/gms/drive/DriveApi.MetadataBufferResult.html"
>{@code DriveApi.MetadataBufferResult}</a>.</p>


<h3 id="Sync">Using synchronous calls</h3>

<p>If you want your code to execute in a strictly defined order, perhaps because the result of one
call is needed as an argument to another, you can make your request synchronous by calling <a
href="{@docRoot}reference/com/google/android/gms/common/api/PendingResult.html#await()"
>{@code await()}</a> on the
<a href="{@docRoot}reference/com/google/android/gms/common/api/PendingResult.html">{@code
PendingResult}</a>. This blocks the thread and returns the <a
href="{@docRoot}reference/com/google/android/gms/common/api/Result.html">{@code Result}</a> object
when the request completes, which is delivered as an instance of the
appropriate subclass as specified by the API you're using, such as <a
href="{@docRoot}reference/com/google/android/gms/drive/DriveApi.MetadataBufferResult.html"
>{@code DriveApi.MetadataBufferResult}</a>.</p>

<p>Because calling <a
href="{@docRoot}reference/com/google/android/gms/common/api/PendingResult.html#await()"
>{@code await()}</a> blocks the thread until the result arrives, it's important that you
never perform this call on the UI thread. So, if you want to perform synchronous requests to a
Google Play service, you should create a new thread, such as with {@link android.os.AsyncTask} in
which to perform the request. For example, here's how to perform the same file request to Google
Drive as a synchronous call:</p>

<pre>
private void loadFile(String filename) {
    new GetFileTask().execute(filename);
}

private class GetFileTask extends AsyncTask&lt;String, Void, Void> {
    protected void doInBackground(String filename) {
        Query query = new Query.Builder()
                .addFilter(Filters.eq(SearchableField.TITLE, filename))
                .build();
        // Invoke the query synchronously
        DriveApi.MetadataBufferResult result =
                Drive.DriveApi.query(mGoogleApiClient, query).await();

        // Continue doing other stuff synchronously
        ...
    }
}
</pre>

<p class="note">
<strong>Tip:</strong> You can also enqueue read requests while not connected to Google Play
services. For example, execute a method to read a file from Google Drive regardless of whether your
Google API Client is connected yet. Then once a connection is established, the read requests
execute and you'll receive the results. Any write requests, however, will generate an error if you
call them while your Google API Client is not connected.</p>