Android谷歌驱动器集成 - 驱动器不连接

问题描述:

我是新来的机器人,并试图使用谷歌驱动器来存储和检索数据。我写了一个代码连接谷歌驱动器。它显示帐户选择器对话框和选择帐户什么都没有发生。Android谷歌驱动器集成 - 驱动器不连接

public class SyncActivity extends MainActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener 
{ 
    GoogleApiClient googleApiClient; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_sync); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     googleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(Drive.API) 
       .addScope(Drive.SCOPE_FILE) 
       .addScope(Drive.SCOPE_APPFOLDER) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 

     Button btnConnectDrive = (Button)findViewById(R.id.connectDrive); 
     btnConnectDrive.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       try{ 
        googleApiClient.connect(); 
       } 
       catch(Exception e){ 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     super.onConnected(bundle); 
     System.out.println("Connected!!!!!!!!!!!!!!!"); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     super.onConnectionSuspended(i); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     super.onConnectionFailed(connectionResult); 
     if(connectionResult.hasResolution()){ 
      try { 
       connectionResult.startResolutionForResult(this, ConnectionResult.RESOLUTION_REQUIRED); 
      } catch (IntentSender.SendIntentException e) { 
       // Unable to resolve, message user appropriately 
       e.printStackTrace(); 
      } 
     } 
     else { 
      GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show(); 
     } 
    } 
} 

您需要按照设置SHA1指纹和从Google开发者控制台获取凭据那样执行一系列步骤。按照Android Quickstart中指示的步骤,也提供了代码片段。

如果您想要快速参考代码,请下载Android Demo for Drive API。我能够在Android设备上运行它。

下面是对帐户选择一个片段:

/** 
    * Called when an activity launched here (specifically, AccountPicker 
    * and authorization) exits, giving you the requestCode you started it with, 
    * the resultCode it returned, and any additional data from it. 
    * @param requestCode code indicating which activity result is incoming. 
    * @param resultCode code indicating the result of the incoming 
    *  activity result. 
    * @param data Intent (containing result data) returned by incoming 
    *  activity result. 
    */ 
    @Override 
    protected void onActivityResult(
      int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     switch(requestCode) { 
      case REQUEST_GOOGLE_PLAY_SERVICES: 
       if (resultCode != RESULT_OK) { 
        mOutputText.setText(
          "This app requires Google Play Services. Please install " + 
          "Google Play Services on your device and relaunch this app."); 
       } else { 
        getResultsFromApi(); 
       } 
       break; 
      case REQUEST_ACCOUNT_PICKER: 
       if (resultCode == RESULT_OK && data != null && 
         data.getExtras() != null) { 
        String accountName = 
          data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); 
        if (accountName != null) { 
         SharedPreferences settings = 
           getPreferences(Context.MODE_PRIVATE); 
         SharedPreferences.Editor editor = settings.edit(); 
         editor.putString(PREF_ACCOUNT_NAME, accountName); 
         editor.apply(); 
         mCredential.setSelectedAccountName(accountName); 
         getResultsFromApi(); 
        } 
       } 
       break; 
      case REQUEST_AUTHORIZATION: 
       if (resultCode == RESULT_OK) { 
        getResultsFromApi(); 
       } 
       break; 
     } 
    }