title: Android: how to make my app remember Google Drive Authorization?
body: <p>I got this code from the Internet and I modified it to upload a specific file automatically  to Google Drive when starting the activity.
  It is always ask me to select my Google account when I start this activity!
  I want it ask about Google account once only when started for the first time after install it, How to do that?</p>
  
  <pre><code>package com.example.googledrive;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.Menu;
  import java.util.Arrays;
  import java.io.IOException;
  import android.accounts.AccountManager;
  import android.content.ContentResolver;
  import android.content.Context;
  import android.content.Intent;
  import android.net.Uri;
  import android.widget.Toast;
  import com.google.api.client.extensions.android.http.AndroidHttp;
  import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
  import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
  import com.google.api.client.http.FileContent;
  import com.google.api.client.json.gson.GsonFactory;
  import com.google.api.services.drive.Drive;
  import com.google.api.services.drive.DriveScopes;
  import com.google.api.services.drive.model.File;
  
  public class MainActivity extends Activity {
      static final int                REQUEST_ACCOUNT_PICKER = 1;
      static final int                REQUEST_AUTHORIZATION = 2;
      static final int                REQUEST_DOWNLOAD_FILE = 3;
      static final int                RESULT_STORE_FILE = 4;
      private static Uri              mFileUri;
      private static Drive            mService;
      private GoogleAccountCredential mCredential;
      private Context                 mContext;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
  
          // setup for credentials for connecting to the Google Drive account
          mCredential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE));
  
          // start activity that prompts the user for their google drive account
          startActivityForResult(mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);        
         // mContext = getApplicationContext(); 
          setContentView(R.layout.activity_main);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu)
      {
          getMenuInflater().inflate(R.menu.main, menu);       
          return true;
      }
  
      @Override
      protected void onActivityResult(final int requestCode, final int resultCode, final Intent data)
      {
          switch (requestCode) {
              case REQUEST_ACCOUNT_PICKER:
                  if (resultCode == RESULT_OK &amp;&amp; data != null &amp;&amp; data.getExtras() != null) {
                      String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                      if (accountName != null) {
                          mCredential.setSelectedAccountName(accountName);
                          mService = getDriveService(mCredential);
                      }
                      saveFileToDrive();
                  }
                  break;
              case REQUEST_AUTHORIZATION:
                  if (resultCode == Activity.RESULT_OK) {
                      //account already picked
                  } else {
                      startActivityForResult(mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
                  }
                  break;
          }
      }
  
      private Drive getDriveService(GoogleAccountCredential credential)
      {
          return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
              .build();
      }
  
  
      private void saveFileToDrive()
      {
          Thread t = new Thread(new Runnable() {
              @Override
              public void run() {
                  try {
                      // Create URI from real path
                      String path;
                      path = "/sdcard/DCIM/Camera/a.png";
                      mFileUri = Uri.fromFile(new java.io.File(path));
  
                      ContentResolver cR = MainActivity.this.getContentResolver();
  
                      // File's binary content
                      java.io.File fileContent = new java.io.File(mFileUri.getPath());
                      FileContent mediaContent = new FileContent(cR.getType(mFileUri), fileContent);
  
                      showToast("Selected " + mFileUri.getPath() + "to upload");
  
                      // File's meta data. 
                      File body = new File();
                      body.setTitle(fileContent.getName());
                      body.setMimeType(cR.getType(mFileUri));
  
                      com.google.api.services.drive.Drive.Files f1 = mService.files();
                      com.google.api.services.drive.Drive.Files.Insert i1 = f1.insert(body, mediaContent);
                      File file = i1.execute();
  
                      if (file != null) 
                      {
                          showToast("Uploaded: " + file.getTitle());
                      }
                  } catch (UserRecoverableAuthIOException e) {
                      startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                  } catch (IOException e) {
                      e.printStackTrace();
                      showToast("Transfer ERROR: " + e.toString());
                  }
              }
          });
          t.start();
      }
  
      public void showToast(final String toast)
      {
          runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
              }
          });
      }
  
  }
  </code></pre>