【问题标题】:How can I add an Account with AddAccountExplicitly?如何显式添加带有广告帐户的帐户?
【发布时间】:2014-11-20 22:58:18
【问题描述】:

我发现了很多关于这个主题的问题,没有答案或外部链接损坏。我想以一种简单的方式添加一个帐户。错误总是一样的:java.lang.SecurityException: caller uid xxxxx is different than the authenticator's uid(我在这里调用 addAccountExplicitly)。但是账号类型在代码和xml中是一样的,所以……我哪里做错了?

public class AuthActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth);

        AccountManager accountManager = AccountManager.get(this);
        final Account account = new Account("username", getString(R.string.account_type));
        accountManager.addAccountExplicitly(account, "password", null);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="@string/account_type"
    android:icon="@drawable/ic_launcher"
    android:smallIcon="@drawable/ic_launcher"
    android:label="@string/name_app" />

【问题讨论】:

  • 只是为了确保您尝试添加自己的帐户类型,而不是 Google、Facebook 或类似帐户?
  • 是的,我自然会尝试添加自定义类型的帐户。

标签: android account accountmanager securityexception


【解决方案1】:

我和你有同样的问题。原来我没有在我的应用程序清单文件中正确注册我的身份验证服务:

<service
        android:name="com.example.android.syncadapter.AuthenticatorService">
    <intent-filter>
        <action android:name="android.accounts.AccountAuthenticator"/>
    </intent-filter>
    <meta-data
        android:name="android.accounts.AccountAuthenticator"
        android:resource="@xml/authenticator" />
</service>

您的意图过滤器名称由 android 框架定义。所以它必须是“android.accounts.AccountAuthenticator”

【讨论】:

    【解决方案2】:

    作为例外情况,您的应用程序和您的身份验证器必须共享相同的 UID。通常,所有不同的应用都会获得不同的 UID,因此如果您没有专门设置它们以共享 UID,它们将拥有不同的 UID。

    您可以通过查看ApplicationInfo.uid 来检查为您的应用分配的 UID。您可以通过在PackageManager 中使用getApplicationInfo() 来获取ApplicationInfo。

    您可以使用清单文件中的 android:sharedUserId 属性为您的身份验证器和您的应用设置共享 UID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多