Im trying to write my own Authenticator and using it as a library with two different apps: A and B. I have followed this post: http://udinic.wordpress.com/2013/04/24/write-your-own-android-authenticator/. I installed app A and then app B. When app A calls AccountManager.addAccount() the AccountAuthenticatorActivity opens. When app B calles AccountManager.addAccount() nothing happenes. If I uninstall app A and try again in app B, AccountAuthenticatorActivity will open.

My goal is to use the same AccountAuthenticatorActivity and AccountAuthenticator for the two apps but it seems to work only at one app at a time.

This is addAccount in my AbstractAccountAuthenticator:

 @Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {

    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AuthenticatorActivity.ARG_ACCOUNT_TYPE, accountType);
    intent.putExtra(AuthenticatorActivity.ARG_AUTH_TYPE, authTokenType);
    intent.putExtra(AuthenticatorActivity.ARG_IS_ADDING_NEW_ACCOUNT, true);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);

    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

This is how I call accountManager.addAccount() from both of my apps:

 private void addNewAccount(String accountType, String authTokenType) {
    final AccountManagerFuture<Bundle> future = mAccountManager.addAccount(accountType, authTokenType, null, null, this, new AccountManagerCallback<Bundle>() {
        @Override
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                Bundle bnd = future.getResult();

            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }, null);
}
有帮助吗?

解决方案

I was having the same problem and the key to all this mess is the AndroidManifest.xml

Tutorials on how to create your custom authenticator are kind of old (or at least pre Android Studio) so they state that you must copy the permissions, activity and service from the authenticator library to all the applications that are going to use the library THIS IS WRONG FOR ANDROID STUDIO because the Android Studio automatically merges the manifests from all modules.

If you are using Android Studio all you need to do is let the permissions, activities and service in the authenticator module and simply add a dependency to that module in the applications.

Here's a sample AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:label="@string/auth_app_name"
    android:theme="@style/Holo.Theme.Light.DarkActionBar">

    <activity
        android:name="ro.muerte.modules.auth.MyAuthenticatorActivity"
        android:exported="true"
        android:label="@string/auth_action_login"
        android:windowSoftInputMode="adjustResize|stateVisible"
        android:clearTaskOnLaunch="true" />


    <service
        android:name="ro.muerte.modules.auth.MyAuthenticateService"
        android:exported="true">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>
        <meta-data
            android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" />
    </service>

</application>

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top