2016-04-27 90 views
0

在這個android應用程序中,我想從授權的Google帳戶獲取用戶數據(電子郵件ID,姓名等)。在這種情況下,我會緩存令牌以查看用戶是否已登錄,如果用戶已經登錄,它將獲取基本用戶數據。從授權的Google帳戶獲取基本的用戶數據

該代碼使用一個按鈕進行登錄。

public void login(View view){ 

    if (loadUserTokenCache(mClient)){ 
     TextView tv1 = (TextView)findViewById(R.id.textView2); 
     tv1.setVisibility(View.VISIBLE); 
    } 
    else { 
     ListenableFuture<MobileServiceUser> mLogin = mClient.login(MobileServiceAuthenticationProvider.Google); 

     Futures.addCallback(mLogin, new FutureCallback<MobileServiceUser>() { 
      @Override 
      public void onFailure(Throwable exc) { 
       createAndShowDialog("You must log in. Login Required", "Error"); 
      } 
      @Override 
      public void onSuccess(MobileServiceUser user) { 
       createAndShowDialog(String.format(
         "You are now logged in - %1$2s", 
         user.getUserId()), "Success"); 
       cacheUserToken(mClient.getCurrentUser()); 
      } 
     }); 

    } 
} 

回答

0

您可以使用AccountsManager進行此操作。例如,這就是你如何檢索用戶的Gmail。

// Retrieve the gmail associated with the device that is being used. 
    String gmailID = ""; 
    Account[] accounts = AccountManager.get(getActivity()).getAccountsByType("com.google"); 
    if(accounts.length > 0) { 
     gmailID = accounts[0].name; 
    }