3

I am following a tutorial for adding a user account to the Android AccountManager.

In my main activity, I have the following method:

private void addNewAccount(String accountType, String authTokenType) {
    Log.d(TAG,"addNewAccount called");
    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();
                Log.d("ACME", "AddNewAccount Bundle is " + bnd);

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

This method is being called, as I see the log in the logcat. Now my AbstractAccountAuthenticator implementation is as follows:

public class AcmeAuthenticator extends AbstractAccountAuthenticator {

private String TAG = "AcmeAuthenticator";
private final Context mContext;

public AcmeAuthenticator(Context context) {
    super(context);
    this.mContext = context;
}

@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
    Log.d("acme", TAG + "> addAccount");

    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;
}

The above method is never called. The following is the service I have created for it:

public class AcmeAuthenticatorService extends Service {
@Override
public IBinder onBind(Intent intent) {

    AcmeAuthenticator authenticator = new AcmeAuthenticator(this);
    return authenticator.getIBinder();
}
}

And my manifest definition is as follows:

<activity android:name="com.exercise.accountmanagerstudy.accountAuthenticator.AuthenticatorActivity" android:label="@string/login_label"/>
    <service android:name=".accountAuthenticator.AcmeAuthenticatorService">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>
        <meta-data android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" />
    </service>
<!-- client -->
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>

<!-- Authenticator -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>

I am not getting an compiler errors, the addAccount override in the AbstractAccountAuthenticator implementation is not called. from the main activity addNewAccount method. I have researched around for a few links here and here. Any help will be appreciated.

Community
  • 1
  • 1
  • *"addAccount not called from AbstractAccountAuthenticator..."* - if its abstract, then there is no code, so it can't all anything. – jww May 12 '15 at 16:33
  • I have created an implementation of AbstractAccountAuthenticator called AcmeAuthenticator, (second code block). It is not getting called from the implementation or the class that extends AbstractAccountAuthenticator. – Santoshastagi May 12 '15 at 16:35

1 Answers1

5

Ok, so I finally did figure it out. Apparently, the authenticator.xml file for AcmeAuthenticator has a field called accountType:

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

When I was calling addNewAccount in my main activity, I was supposed to pass the exact value of the accountType in the above mentioned xml as the accountType argument. Phew, that took me quite sometime and hope it helps someone else :-).

  • Was having similar issue. I thought AccountManager.KEY_ACCOUNT_TYPE would have picked the value set in authenticator.xml file without me having to pass it myself. – saintjab Mar 12 '20 at 11:11