在特定时间后停止GoogleApiClient连接

时间:2015-07-10 04:59:58

标签: android scheduler google-api-client

目前,我正在使用GoogleApiClient。有时候,GoogleApiClient连接和Game Snapshot开放已经花了很长时间,因为网络速度慢。所以,我想在特定时间之后调用断开连接,因为我不希望用户等待太久。任何人都有相关经验,你能给我一些建议吗?

谢谢和最诚挚的问候!

1 个答案:

答案 0 :(得分:0)

我很久以前就解决了这个问题,但忘掉了这个问题。所以,我再来这里回答我自己的问题。希望能帮助其他人。

.await(YOUR_TIMEOUT, TimeUnit.SECONDS)

完整的API PendingResult.await

void loadFromSnapshot(final SnapshotMetadata snapshotMetadata) {
        AsyncTask<Void, Void, Integer> task = new AsyncTask<Void, Void, Integer>() {
            @Override
            protected Integer doInBackground(Void... params) {
                if (!isGoogleConnected()) {
                    return GamesStatusCodes.STATUS_NETWORK_ERROR_OPERATION_FAILED;
                }
                Snapshots.OpenSnapshotResult result;
                if (snapshotMetadata != null && snapshotMetadata.getUniqueName() != null) {
                    LogUtils.logD(TAG, "Opening snapshot by metadata: " + snapshotMetadata);
                    result = Games.Snapshots.open(mGoogleApiClient, snapshotMetadata).await();
                } else {
                    final boolean isDefaultMode = isDefaultMode();
                    LogUtils.logD(TAG, "Opening current save name " + (isDefaultMode ? mDefaultPlayer : mChildPlayer));
                    if (isDefaultMode) {
                        result = Games.Snapshots.open(mGoogleApiClient, mDefaultPlayer, true).await(READ_TIMEOUT, TimeUnit.SECONDS);
                    } else {
                        result = Games.Snapshots.open(mGoogleApiClient, mChildPlayer, true).await(READ_TIMEOUT, TimeUnit.SECONDS);
                    }
                }
                if (result == null) {
                    return GamesStatusCodes.STATUS_TIMEOUT;
                }
                int status = result.getStatus().getStatusCode();
                Snapshot snapshot = null;
                if (status == GamesStatusCodes.STATUS_OK) {
                    snapshot = result.getSnapshot();
                } else if (status == GamesStatusCodes.STATUS_SNAPSHOT_CONFLICT) {
                    // if there is a conflict - then resolve it.
                    snapshot = processSnapshotOpenResult(RC_LOAD_SNAPSHOT, result, 0);
                    // if it resolved OK, change the status to Ok
                    if (snapshot != null) {
                        status = GamesStatusCodes.STATUS_OK;
                    } else {
                        LogUtils.logD(TAG, "Conflict was not resolved automatically");
                    }
                }
                if (snapshot != null) {
                    try {
                        readSavedGame(snapshot);
                    } catch (IOException e) {
                        LogUtils.logE(TAG, "Error while reading snapshot contents: " + e.getMessage());
                    }
                }
                return status;
            }

            @Override
            protected void onPostExecute(Integer status) {
                // play with your status
            }
        };

        task.execute();