使用Tasks Drive API检查requestSync结果状态

时间:2017-11-16 10:16:33

标签: android google-drive-api google-drive-android-api

使用以前的API(使用PendingResult),我可以执行以下操作:

Drive.DriveApi.requestSync(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() {
          @Override
          public void onResult(@NonNull Status status) {
            if (status.getStatusCode() == DRIVE_RATE_LIMIT_EXCEEDED) {
              //Here I know Drive rate limit was exceeded.
            }
          }
        });

如何使用新的Tasks API完成相同的操作?

我在documentation中读到的内容:

  

如果请求已被速率限制,则操作将失败   DRIVE_RATE_LIMIT_EXCEEDED状态

但是如何查看Task<Void> requestSync的状态?

据我所知:

Task<Void> checkSync = myDriveClient.requestSync();
checkSync.addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
        public void onComplete(@NonNull Task<Void> task) {
            //how do I check the status here?
        }
});

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。实际上它是onFailureListener的异常。 但是我需要将其转换为ApiException。

Task<Void> checkSync = Drive.getDriveClient(getContext(), mGoogleSignInAccount).requestSync();
    checkSync.addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            //how do I check the status here?
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            ApiException apiException = (ApiException) e;
            if (DriveStatusCodes.DRIVE_RATE_LIMIT_EXCEEDED == apiException.getStatusCode()) {
                //Here I know Drive rate limit was exceeded.
            }
        }
    });

希望它有所帮助。

答案 1 :(得分:0)

您可以通过调用task.isSuccessful()来检查requestSync的状态。看来,如果发生DRIVE_RATE_LIMIT_EXCEEDED,无论如何都会首先调用addOnCompleteListener。

myDriveClient.requestSync()
.addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            Log.i(TAG, "requestSync success");
        } else {
            Log.i(TAG, "requestSync not successful");
        }
    })
    .addOnFailureListener(e -> {
        Log.i(TAG, "requestSync failure");
    });