java - 将应用程序移动到 SD 卡时帐户 validator 丢失

标签 java android

当我将我的测试应用移至 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/

相关文章:

java - 如何将 HashMap 的键设置为 XML 文件中的标签?

java - 如何杀死条件边界突变体

java - 调用TimerTask的run()方法

java - 我可以使用 Firestore 检索不同集合中具有相同名称的相同子集合的文档吗?

java - 使用泛型类作为类型参数的上限

android - 我们如何区分闪存和SD卡内存?

android - 摄像头 : setDisplayOrientation function is not working for Samsung Galaxy ACE with Android 2. 3.6

android - Gradle同步意外失败

android - Android 和 Windows Phone 之间的 SSL 连接区别

android - 导航组件 Kotlin - 无法从当前目的地找到