Android AccountManager addAccount from service

1.8k views Asked by At

I'm using an account manager account in a service (which extends FirebaseInstanceIdService). If theres no valid account then I add the account using accountManager.addAccount.

That takes as a parameter activity (which is used for starting the account log-in activity). However as I'm calling addAccount from a service I don't have a current activity to place there. How do I call addAccount from a service and get it to display the account login where needed?

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onTokenRefresh() {
        AccountManager accountManager = (AccountManager) getApplicationContext().getSystemService(ACCOUNT_SERVICE);
        Account account[] = accountManager.getAccountsByType(ACCOUNT_TYPE);
        if(account.length==0) {
            Activity activity=???????//What can I set here
            accountManager.addAccount(ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS, null,
            null, activity, new AccountManagerCallback<Bundle>() {
                        public void run(AccountManagerFuture<Bundle> arg0) {
                        }
                    }, null);
            return null;
        }
        //do stuff with ContentResolver using account
    }
}
1

There are 1 answers

2
Prince Bansal On

addAccount() method requires Activity instance to call the default Authenticator Activity.

Their is another method called addAccountExplicitly() in AccountManager class. Here is the documentation:

/**
     * Adds an account directly to the AccountManager. Normally used by sign-up
     * wizards associated with authenticators, not directly by applications.
     * <p>Calling this method does not update the last authenticated timestamp,
     * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
     * {@link #notifyAccountAuthenticated(Account)} after getting success.
     * However, if this method is called when it is triggered by addAccount() or
     * addAccountAsUser() or similar functions, then there is no need to update
     * timestamp manually as it is updated automatically by framework on
     * successful completion of the mentioned functions.
     * <p>It is safe to call this method from the main thread.
     * <p>This method requires the caller to have a signature match with the
     * authenticator that owns the specified account.
     *
     * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
     * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs
     * for this function in API level 22.
     *
     * @param account The {@link Account} to add
     * @param password The password to associate with the account, null for none
     * @param userdata String values to use for the account's userdata, null for
     *            none
     * @return True if the account was successfully added, false if the account
     *         already exists, the account is null, or another error occurs.
     */
    public boolean addAccountExplicitly(Account account, String password, Bundle userdata); 

Usage:

Create Account instance:

final Account account = new Account(accountName, intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE));

Initialise account metadata:

String authtoken = //Generated Auth Token ;
String authtokenType = //Auth Token type;
String accountPassword= //Auth password if available;

Call addAccountExplicitly() method:

mAccountManager.addAccountExplicitly(account, accountPassword, //User data bundle);
mAccountManager.setAuthToken(account, authtokenType, authtoken);

This will be good to go. Good luck!