帐户身份验证器在将应用程序移至SD卡时丢失

时间:2014-07-24 06:46:17

标签: java android

当我将testapp移动到SD卡时,我的自定义帐户身份验证器(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时(使用以下代码片段):

    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}

任何想法?

1 个答案:

答案 0 :(得分:3)

实现自定义帐户的部分很少......

在您的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数据(负责您的身份验证器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>

最后,您必须定义您的Manifest服务。请不要忘记管理帐户的相关权限(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>