当我将我的测试应用移至 SD 卡时,我的自定义帐户 validator (com.heidi.AccountStuff) 不再存在。
如果我像这样添加一个新帐户
Account account = new Account("heidi", AccountAuthenticatorService.TYPE);
AccountManager accountManager = AccountManager.get(this);
accountManager.addAccountExplicitly(account, "", null);
它会抛出一个RuntimeException
java.lang.SecurityException: caller uid XXXXX is different than the authenticator's uid
好的,有道理,因为当我输出 authenticatortypes 时(使用以下代码 fragment ):
for(AuthenticatorDescription d: accountManager.getAuthenticatorTypes()) {
Log.d("add", d.toString());
}
它会输出
AuthenticatorDescription {type=com.htc.linkedin}
AuthenticatorDescription {type=com.htc.android.mail.eas}
AuthenticatorDescription {type=com.htc.sync.provider.weather}
AuthenticatorDescription {type=com.htc.android.windowslive}
AuthenticatorDescription {type=com.htc.android.mail}
AuthenticatorDescription {type=com.htc.stock}
AuthenticatorDescription {type=com.htc.lucy.account}
AuthenticatorDescription {type=com.google}
将应用移回内部商店后,我的自定义类型显示在输出中,如下所示:
AuthenticatorDescription {type=com.heidi.AccountStuff}
AuthenticatorDescription {type=com.htc.linkedin}
AuthenticatorDescription {type=com.htc.android.mail.eas}
AuthenticatorDescription {type=com.htc.sync.provider.weather}
AuthenticatorDescription {type=com.htc.android.windowslive}
AuthenticatorDescription {type=com.htc.android.mail}
AuthenticatorDescription {type=com.htc.stock}
AuthenticatorDescription {type=com.htc.lucy.account}
AuthenticatorDescription {type=com.google}
有什么想法吗?
最佳答案
实现自定义帐户的部分很少......
要在您的 Activity 中调用 AccountManager,类似于您已经实现的...
Account account = new Account(username, ACCESS_TYPE);
AccountManager am = AccountManager.get(this);
Bundle userdata = new Bundle();
userdata.putString("SERVER", "extra");
if (am.addAccountExplicitly(account, password, userdata)) {
Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, username);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCESS_TYPE);
setAccountAuthenticatorResult(result);
}
在 res/xml/authenticator.xml 中,您必须定义您的 AccountAuthenticator 数据(负责您的 Authenticator UID)。 ACCESS_TYPE 必须与您在此 xml 中定义的 accountType 的字符串相同!
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="de.buecherkiste"
android:icon="@drawable/buecher"
android:label="@string/app_name"
android:smallIcon="@drawable/buecher" >
</account-authenticator>
最后,您必须定义您的服务 list 。请不要忘记管理帐户的相关权限(AUTHENTICATE_ACCOUNTS/USE_CREDENTIALS/GET_ACCOUNTS/MANAGE_ACCOUNTS)
<service android:name="AuthenticatationService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
关于java - 将应用程序移动到 SD 卡时帐户 validator 丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24926812/