addAccountExplicitly throws IllegalStateException

2019-02-18 18:38发布

问题:

Using this code throws that exception and doesn't add created account into account manager.

AccountManager am = AccountManager.get(activity);
Account acc = new Account(name,activity.getString(R.string.account_type));
am.addAccountExplicitly(acc,"Password",null);

I have followed this - http://developer.android.com/training/id-auth/custom_auth.html

Any idea why it is caused?

//edit: Caused by: java.lang.SecurityException: caller uid 10035 is different than the authenticator's uid

回答1:

have you tried setting permissions in the manifest, i suspect you may need this?

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.USE_CREDENTIALS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


回答2:

The documentation for AddAccountExplicitly say:

This method requires the caller to hold the permission AUTHENTICATE_ACCOUNTS and to have the same UID as the added account's authenticator.

The SecurityException that is thrown explains the problem, the UID of the application that is trying to add the account is different from the UID of the authenticator. In other words they are two different applications.

If you want these two to be separate applications you could make sure you share the UID between them. See the android:sharedUserId section of this page for a bit more information on how to do that.