Search code examples
androidaccountaccountmanager

Accountmanager.addAccount() vs Accountmanager.addAccountExplicitly()


why we use Accountmanager.addAccount() when we can create account with all parameters with Accountmanager.addAccountExplicitly().

I googled and find out when we use Accountmanager.addAccount() it trigles AbstractAccountAuthenticator addAccount event but what is the point? why we should using addAccount method?

UPDATED

we can create account in this way:

 Account account = new Account(accountname, accountType);
 mAccountManager.addAccountExplicitly(account, accountPassword, null);

Solution

  • I finally find out after many tries!

    Accountmanager.addAccount() and Accountmanager.addAccountExplicitly() are very different methods!

    when you call Accountmanager.addAccount() it's call a same method that in your AbstractAccountAuthenticator you can handle what happens. in other hand when user go to phone settings/account and select your custom account type and press "add an account" this method will call. so I handle account-type and many stuff and direct user to login/singup page.

    public class MyAuthenticator extends AbstractAccountAuthenticator {
    
        @Override
        public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
    
            final Intent intent = new Intent(mContext, DirectLogin.class);
            intent.putExtra(Constants.ARG_ACCOUNT_TYPE, accountType);
            intent.putExtra(Constants.ARG_AUTH_TYPE, authTokenType);
            intent.putExtra(Constants.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;
        }
    

    then in my activiy user chose to create an account or sign in. after sing-in or sign-up user get tokens from server and action completes and finally I use Accountmanager.addAccountExplicitly() to add account.

        String accountName = "name";
        String accountPassword = "password";
        final Account account = new Account(accountName, "account_type");
    
        AccountManager mAccountManager = AccountManager.get(getBaseContext());
    
        String authToken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
        String refreshToken = intent.getStringExtra(AccountManager.KEY_USERDATA);
        String authTokenType = AccountGeneral.ACCOUNT_TYPE;
        mAccountManager.addAccountExplicitly(account, accountPassword, null);
        mAccountManager.setAuthToken(account, authTokenType, authToken);
        mAccountManager.setUserData(account, "refreshToken", refreshToken);