问题
I have a simple app which is an extended activity that registers a user's primary email account when accessing a specific NFC tag.
Unfortunately, for some reason even though I can see several accounts provisioned on my Samsung device the code below only returns one, and not the full list. The one it returns appears to be the Samsung account ID and not the Google ID.
Is this a simple case of user error or is there some other way to get to the Google ID?
This code has been updated to reflect the proper functionality. Closing this one out.
The code snippet is here:
public class MainActivity extends Activity {
.
.
.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.message);
final Context context = getApplicationContext();
Thread myThread = new Thread(
new Runnable() {
public void run() {
try {
GoogleAuthUtil.requestGoogleAccountsAccess(context);
} catch (Exception e) {
if (e instanceof UserRecoverableAuthException) {
startActivityForResult(((UserRecoverableAuthException)
e).getIntent(), MY_PERMISSIONS_REQUEST_READ_CONTACTS);
} else {
Log.e("SignIn", "Exception in getting google accounts" + e);
}
}
}
});
myThread.start();
AccountManager accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccounts();
ClientRequest request = new ClientRequest (accounts);
handleIntent(getIntent());
}
.
.
.
}
This is what I am seeing it return in the debugger, where foo@bar.net is the Samsung ID on my test device:
accounts[0] = {Account@4923} "Account {name=foo@bar.net, type=com.osp.app.signin}"
this = {MainActivity@4856}
savedInstanceState = null
context = {Application@4867}
accountManager = {AccountManager@4903}
accounts = {Account[1]@4913}
0 = {Account@4923} "Account {name=foo@bar.net, type=com.osp.app.signin}"
来源:https://stackoverflow.com/questions/58087651/accountmanager-only-returning-one-account-in-extended-activity