目前,我正在使用GoogleApiClient。有時候,GoogleApiClient連接和Game Snapshot打開需要很長時間,因爲網絡速度很慢。所以,我想在特定時間後斷開它,因爲我不希望用戶等待太久。任何人都有經驗,你可以給我一些建議!停止在特定時間後連接GoogleApiClient
謝謝,最好的問候!
目前,我正在使用GoogleApiClient。有時候,GoogleApiClient連接和Game Snapshot打開需要很長時間,因爲網絡速度很慢。所以,我想在特定時間後斷開它,因爲我不希望用戶等待太久。任何人都有經驗,你可以給我一些建議!停止在特定時間後連接GoogleApiClient
謝謝,最好的問候!
我已經解決了很久以前的問題,但忘記了這個問題。所以,我再來這裏回答我自己的問題。希望幫助其他人。
.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();
嗨@John史蒂夫,這可能有點晚了。你能顯示整個代碼嗎? –
@PaulaKristin,代碼是什麼,兄弟? – NamNH
這個解決方案,你剛剛放在。你可以顯示使用'.await(YOUR_TIMEOUT,TimeUnit.SECONDS)' –
使用CountDownTimer或任何其他低級別的Java調度機制的接收信號時,時間已經ellapsed,並在您的處理程序,調用Disconnect()。 –