Here in this demo we will try to create a Custom authenticator.
The account manager is a centralized service offered by Android system. Any application can get the list of accounts and request the user to utilize its auth tokens.
Basically it contains a list of Accounts, each one is identified by:
1. Account name: The user name used to log in. For example, CoderzHeaven
2. Account type: The type of the account. For example, com.google
All the accounts should be unique across a device. This is, for a given device, there cannot be any two accounts that have the same account name and account type.
For each one of the accounts, there is a set of data related with it:
1. Password: The password of the account. It could be empty also.
2. AuthTokens: The String used by the server to identify the user, instead of the password. Normally the auth token is temporary and it will be expired after a time. All the auth tokens have a type, called AuthTokenType. This is because one account could be used for several services, and for each one of the service there could be different auth tokens. For example, the account of Google could be used for Gmail and Youtube. The authTokenType of Gmail is “mail” and the authTokenType of YouTube is youtube.3. UserDatas: Additionally the user can save user data as the pair key/value of type String. This is useful when extra data are associated with the account whom are not password neither auth token.
The applications cannot creates a new account manager but get the existence one passing the context.
AccountManager accountManager = AccountManager.get(Context)
Add a new account
The easiest way to add a new account is utilize the method:
boolean addAccountExplicitly(Account account, String password, Bundle userdata)
Notices the class Account could be easily built with:
Account account = new Account(userName, accountType)
Both of the parameters are String.
for eg: userName can be “CoderzHeaven” and accountType can be “com.coderzheaven.accounts”.
For creating a new account you just need to call below sample code with correct parameters.
Bundle data = new Bundle(); data.putString(AccountManager.KEY_ACCOUNT_NAME, accountName); data.putString(AccountManager.KEY_ACCOUNT_TYPE, mAuthTokenType); data.putString(AccountManager.KEY_AUTHTOKEN, authtoken); data.putString(PARAM_USER_PASS, password); // Some extra data about the user Bundle userData = new Bundle(); userData.putString("UserID", "25"); data.putBundle(AccountManager.KEY_USERDATA, userData); //Make it an intent to be passed back to the Android Authenticator final Intent res = new Intent(); res.putExtras(data); //Create the new account with Account Name and TYPE final Account account = new Account(accountName, mAuthTokenType); //Add the account to the Android System if (mAccountManager.addAccountExplicitly(account, password, userData)) { // worked Log.d(TAG, "Account added"); mAccountManager.setAuthToken(account, mAuthTokenType, authtoken); setAccountAuthenticatorResult(data); setResult(RESULT_OK, res); finish(); } else { // guess not Log.d(TAG, "Account NOT added"); }
The complete source code is available here…
Pingback: Sync Adapter in Android - A Simple Example...
Pingback: Sync Adapter in Android – A Simple Example… | All Things Gadget
Pingback: Sync Adapter in Android – A Simple Example… – CoderzHeaven - Jacob is studying on web programming
Pingback: Sync Adapter in Android – A Simple Example… – CoderzHeaven - Jacob is studying on programming