2016-07-09 5 views
2

いくつかのフォルダを作成し、ユーザーのためにいくつかのデータを含むテキストファイルをアップロードするためにDriveApiを使用しようとしています。GoogleApiClient onConnectedコールバックが正しく呼び出されない

私は(link)からのクイックスタートガイドを実装しようとしましたが、それはいくつかの基本的な問題があります:APIはonResumeで接続されますので、ユーザがアクセス権を与えることを促されます

  1. をアプリを開いた直後にアプリが混乱して恐ろしいです。
  2. 同意画面で戻るボタンを拒否または押すと、onResumeメソッドが再度呼び出され、同意画面がもう一度表示され、無限ループにつながります。

ユーザーが実際にデータを格納する必要がある場合、私はむしろユーザーにもっと理解しやすいようにしたいと考えています。私は、ユーザーが時にAPI実行しようとしました何の参照を維持することを期待していた

@Override 
protected void onResume() { 
    super.onResume(); 
    if (mGoogleApiClient == null) { 
     // Create the API client and bind it to an instance variable. 
     // We use this instance as the callback for connection and connection 
     // failures. 
     // Since no account name is passed, the user is prompted to choose. 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(Drive.API) 
       .addScope(Drive.SCOPE_FILE) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 

     mDriveHelper = new DriveApiHelper(mGoogleApiClient); 
    } 

    //Log.d(TAG, mDriveHelper.getCurrentAction() + ""); 
    Log.d("test", "Connected " + mGoogleApiClient.isConnected()); 
} 

@Override 
public void onConnected(Bundle connectionHint) { 
    Log.i(TAG, "API client connected."); 
    switch (mDriveHelper.getCurrentAction()) { //resume the last action 
     case CREATING_FOLDER: 
      mDriveHelper.createBackupsFolder(); 
      break; 
    } 
} 

:私はこのようにそれをやってみました:

ResultCallback<DriveFolder.DriveFolderResult> folderCreatedCallback = new 
     ResultCallback<DriveFolder.DriveFolderResult>() { 
      @Override 
      public void onResult(@NonNull DriveFolder.DriveFolderResult result) { 
       clearCurrentAction(); 

       if (!result.getStatus().isSuccess()) { 
        Log.e(TAG, "Error while trying to create the folder"); 
        return; 
       } 
       Log.d(TAG, "Created a folder: " + result.getDriveFolder().getDriveId()); 
      } 
     }; 

public DriveApiHelper(GoogleApiClient mGoogleApiClient) { 
    this.mGoogleApiClient = mGoogleApiClient; 
} 

public void createBackupsFolder() { 
    currentAction = DriveActions.CREATING_FOLDER; 

    if (mGoogleApiClient.isConnected()) { 
     MetadataChangeSet changeSet = new MetadataChangeSet.Builder() 
       .setTitle("test").build(); 
     Drive.DriveApi.getRootFolder(mGoogleApiClient).createFolder(
       mGoogleApiClient, changeSet).setResultCallback(folderCreatedCallback); 
    } else { 
     mGoogleApiClient.connect(); 
    } 
} 

、これが私のonResumeonConnected方法がどのように見えるかです接続するように頼まれた、私はAPIに正常に接続した後、そのアクションを再開することができます。これは私のニーズに合った最も近い実装ですが、実際に同意画面から「許可」ボタンをクリックした後でも、APIコールバックは呼び出されません(onConnected,)。

実際に接続するにはもう一度connectメソッドを呼び出し、onConnectedはユーザーの操作を正常に再開する必要があります。

+0

あなたは解決策を手に入れましたか? – SimpleCoder

+0

@SimpleCoder 私が覚えているように、私は 'onActivityResult'をオーバーライドしていませんでした。 –

答えて

0

私はちょうど追加onActivityResult(それはその時にドキュメントに記載されなかったと、彼らは今、それが含まれている場合、私は知らない)

をオーバーライドし忘れていることが判明:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     if (requestCode == REQUEST_CODE_RESOLUTION) { 
      mGoogleApiClient.connect(); 
     } 
    } else { 
     if (requestCode == REQUEST_CODE_RESOLUTION) { 
      mDriveHelper.dismissStatusDialog(); 
     } 
    } 
} 
関連する問題