2017-08-02 63 views
1

我正在使用CONTACTS组中的GET_ACCOUNTS权限,并且还在我的代码中处理它。我只是不知道为什么帐户名称设置后仍为空。该应用程序已具有权限,因为没有弹出对话框来告诉用户授予权限。GoogleAccountCredential getSelectedAccountName()在设置并执行权限后返回空值

我将凭证在OnCreate像这样:

 mCredential = GoogleAccountCredential.usingOAuth2(
       getApplicationContext(), Arrays.asList(SCOPES)) 
       .setBackOff(new ExponentialBackOff()); 

这里就是我试图设置名称:

/** 
* Attempts to set the account used with the API credentials. If an account 
* name was previously saved it will use that one; otherwise an account 
* picker dialog will be shown to the user. Note that the setting the 
* account to use with the credentials object requires the app to have the 
* GET_ACCOUNTS permission, which is requested here if it is not already 
* present. The AfterPermissionGranted annotation indicates that this 
* function will be rerun automatically whenever the GET_ACCOUNTS permission 
* is granted. 
*/ 
@AfterPermissionGranted(REQUEST_PERMISSION_GET_ACCOUNTS) 
    private void chooseAccount() { 
     if (EasyPermissions.hasPermissions(
       this, Manifest.permission.GET_ACCOUNTS)) { 

      String accountName = getPreferences(Context.MODE_PRIVATE) 
        .getString(PREF_ACCOUNT_NAME, null); 
      if (accountName != null) { 
       mCredential.setSelectedAccountName(accountName); // I set the account name here 

       Log.d("MESSAGE", accountName); // this prints out the account name as an email 
       Log.d("MESSAGE", mCredential.getSelectedAccountName()); // this throws a null pointer 

       getResultsFromApi(); 
      } else { 
       // Start a dialog from which the user can choose an account 
       Log.d("MESSAGE", "Start dialogue box to select account"); 
       startActivityForResult(
         mCredential.newChooseAccountIntent(), 
         REQUEST_ACCOUNT_PICKER); 
      } 
     } else { 
      // Request the GET_ACCOUNTS permission via a user dialog 
      Log.d("MESSAGE", "Creates dialogue box to request permission"); 
      EasyPermissions.requestPermissions(
        this, 
        "This app needs to access your Google account (via Contacts).", 
        REQUEST_PERMISSION_GET_ACCOUNTS, 
        Manifest.permission.GET_ACCOUNTS); 
     } 
    } 

以下是清单文件我的权限:

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 

这是我第一个项目,我不是很有经验。任何帮助将不胜感激!!

+0

您正在使用首选项来获取帐户名称,但您尚未将其保存在任何地方。你有没有把它保存在偏好中? –

+0

是的,它保存在首次启动应用程序时调用的方法中创建的首选项。它在从用户请求帐户名称后,将该帐户名称保存在该首选项中。上述方法访问已存在的首选项。帐户名称保存在字符串中。它只是不会让我把它设置为凭证。 –

+0

在mCredential.setSelectedAccountName(accountName)中传递它之前,accountName的值是多少? ? – noogui

回答

0

我有同样的问题。用户setSelectedAccount()而不是setSelectedAccountName()。

mCredential = GoogleAccountCredential.usingOAuth2(
       getApplicationContext(), Arrays.asList(SCOPES)) 
       .setBackOff(new ExponentialBackOff()); 

     // to set accountName manually instead of prompting user to select it 
     mCredential.setSelectedAccount(new Account("[email protected]", "come.android.example")); 

将gmail帐户作为第一个参数,并将包名称作为第二个参数。它应该工作。