2011-05-07 49 views

回答

3
  1. AccountManager從API Level 5開始可用,這意味着所有使用Android 2.0或更高版本的設備都會擁有它。

  2. 您可以使用getAccountsByTypecom.google作爲帳戶類型來檢查Google帳戶。

  3. 即使設備具有Android 2.0或更高版本,也無法保證用戶將設置Google帳戶。他們將無法訪問市場或其他谷歌應用程序(Gmail,地圖等),但其他任何工作。

就像谷歌那樣做:當用戶啓動應用程序時,檢查是否有正確的帳戶,如果不通知用戶並停止應用程序。

+0

假定設備已安裝(在Android 2.0的頂部)谷歌API那麼我們應該能夠重定向用戶的設置來設置一個新的帳戶。我需要一種方法來確定是否已安裝Google身份驗證程序的附加組件,而不是查明是否已設置任何Google帳戶。 – mhdwrk 2011-05-07 20:34:13

+0

在Android模擬器上,只有在平臺2.2或更高版本上將目標設置爲Google API(級別8)時,「添加Google帳戶」選項纔會顯示在設置中! – mhdwrk 2011-05-07 20:48:09

0

這不是隻涉及谷歌帳戶身份驗證,這種行爲是一般:

AccountManager.get(context).addAccount(
        <google account type>, 
        <needed token type>, 
        null, 
        <options or null if not needed>, 
        activityToStartAccountAddActivity, 
        new AccountManagerCallback<Bundle>() { 
         @Override 
         public void run(AccountManagerFuture<Bundle> future { 
          try { 
           future.getResult(); 
          } catch (OperationCanceledException e) { 
           throw new RuntimeException(e); 
          } catch (IOException e) { 
           throw new RuntimeException(e); 
          } catch (AuthenticatorException e) { 
           throw new RuntimeException(e); // you'll go here with "bind failure" if google account authenticator is not installed 
          } 
         } 
        }, 
        null); 

如果你沒有在設備上安裝身份驗證,同時支持請求的帳戶類型和令牌類型,那麼你」會得到AuthenticatorException。基本上,任何Android設備都有谷歌驗證器。如果它不是根源,並刪除相關軟件包,當然:)

+0

它不僅與Google帳戶身份驗證程序相關,當沒有身份驗證程序註冊所需的標記類型時會收到此行爲,並將其作爲參數傳遞 – Deepscorn 2015-06-18 10:01:51

0

使用getAccountsByType的解決方案的問題是,您不能區分未驗證者的情況下或驗證者的存在,但缺乏帳戶通過它認證。在第二種情況下,您可能希望提示用戶添加新帳戶。

試圖添加一個帳戶,然後檢查異常也不太理想,當方法AccountManager.getAuthenticatorTypes()存在。使用它像這樣:

String type = "com.example"; // Account type of target authenticator 
AccountManager am = AccountManager.get(this); 
AuthenticatorDescription[] authenticators = am.getAuthenticatorTypes(); 
for (int i = 0; i < authenticators.length(); ++i) { 
    if (authenticators[i].type.equals(type)) { 
     return true; // Authenticator for accounts of type "com.example" exists. 
    } 
return false; // no authenticator was found. 

我的Java是一個有點生疏了(我是一個Xamarin開發者),但是這應該給你如何檢查系統上的認證存在的想法不觸發加帳戶活動,如果它確實存在。

來源:
getAuthenticatorTypes
AuthenticatorDescription.type

相關問題