addAccountExplicitly throws IllegalStateException

2019-02-18 18:23发布

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

2条回答
可以哭但决不认输i
2楼-- · 2019-02-18 18:47

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-18 18:53

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>
查看更多
登录 后发表回答