Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 936
Mahdi Avatar asked Apr 01 '16 09:04

Mahdi


2 Answers

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);
like image 182
Mahdi Avatar answered Oct 11 '22 11:10

Mahdi


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.

like image 30
ben75 Avatar answered Oct 11 '22 11:10

ben75