Accountmanager.addAccount() vs Accountmanager.addAccountExplicitly()
Asked Answered
C

2

8

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);
Corotto answered 1/4, 2016 at 9:55 Comment(0)
C
13

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);
Corotto answered 8/5, 2016 at 11:27 Comment(2)
In short, addAccount () shows a screen where you can add an account, and addAccountExplicitly () really adds an account. Right?Matthaeus
@Matthaeus Exactly!Corotto
A
2

Accountmanager.addAccount() must be use to ask the user to create an account of some type. The user have to approve and interact with the device so that the account get indeed created. This method can be use to create an account of any type.

AccountManager.addAccountExplicitely() allows you to create an Account without user approval/interaction, but it is limited to account type for which the authenticator have the same signature as yours.

Apraxia answered 3/4, 2016 at 15:57 Comment(1)
I updated the question. we can also add account type without need of addAccount()Corotto

© 2022 - 2024 — McMap. All rights reserved.