在特定时间后停止 GoogleApiClient 连接
Stop GoogleApiClient connecting after specific time
目前,我正在使用 GoogleApiClient。有时,由于网络慢,GoogleApiClient连接和游戏快照打开需要很长时间。所以,我想在特定时间后调用断开连接,因为我不想让用户等待太久。有没有人有这方面的经验,可以给我一些建议吗!
谢谢并致以最诚挚的问候!
我很久以前就有解决方案了,但忘记了这个问题。所以,我又来这里回答我自己的问题。希望对其他人有所帮助。
.await(YOUR_TIMEOUT, TimeUnit.SECONDS)
完整APIPendingResult.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();
目前,我正在使用 GoogleApiClient。有时,由于网络慢,GoogleApiClient连接和游戏快照打开需要很长时间。所以,我想在特定时间后调用断开连接,因为我不想让用户等待太久。有没有人有这方面的经验,可以给我一些建议吗!
谢谢并致以最诚挚的问候!
我很久以前就有解决方案了,但忘记了这个问题。所以,我又来这里回答我自己的问题。希望对其他人有所帮助。
.await(YOUR_TIMEOUT, TimeUnit.SECONDS)
完整APIPendingResult.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();