在我开始之前,我想强调一下,我读了一些答案但他们没有帮助我。像:
addAccountExplicitly throws IllegalStateException caused by Securityexception
SecurityException: caller uid XXXX is different than the authenticator's uid ...
像标题建议一样,我试图在应用程序(不是服务)下的Activty中使用AccountManager,但没有成功。我使用4.3模拟器(英特尔图像)。我可以轻松地在模拟器上清除所有帐户(通过android界面添加)但是当我想创建一个时出现错误:09-14 13:21:21.863: E/AndroidRuntime(3291): java.lang.SecurityException: caller uid 10046 is different than the authenticator's uid
09-14 13:21:21.863: E/AndroidRuntime(3291): at android.os.Parcel.readException(Parcel.java:1431)
09-14 13:21:21.863: E/AndroidRuntime(3291): at android.os.Parcel.readException(Parcel.java:1385)
09-14 13:21:21.863: E/AndroidRuntime(3291): at android.accounts.IAccountManager$Stub$Proxy.addAccountExplicitly(IAccountManager.java:719)
09-14 13:21:21.863: E/AndroidRuntime(3291): at android.accounts.AccountManager.addAccountExplicitly(AccountManager.java:612)
以下是我的快速代码段:
Login.java - >
public class Login extends AccountAuthenticatorActivity {
...
protected void onCreate(Bundle savedInstanceState) {
...
AccountManager accountmanager = AccountManager.get(this);
Account userAccount = new Account(“Test”, “test.activities”);
if (accountmanager.addAccountExplicitly(userAccount, “pass”, null))
System.out.println(“Added success”);
else System.out.println(“Added failed”);
}
...
}
RES / XML / authenticator.xml
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="test.activities"
android:icon="@drawable/ic_launcher"
android:smallIcon="@drawable/ic_launcher"
android:label="@string/app_name"
/>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxxx"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.USE_CREDENTIALS"></uses-permission>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:name="Application"
android:allowBackup="true"
android:icon="@drawable/cg_icon_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
...
</application>
</manifest>
在2.2 Android手机上试过,错误是一样的。我可以成功列出所有帐户,但是(使用帐户[] accounts = accountmanager.getAccounts(); )。 我也试过了这个应用https://play.google.com/store/apps/details?id=com.udinic.accounts_example并可以添加该帐户。但是通过我的申请,没有成功。