Everyone likes it when you remember their name. One of the simplest, mosteffective things you can do to make your app more lovable is to remember whoyour user is—especially when the user upgrades to a new device or starts carryinga tablet as well as a phone. But how do you know who your user is? And how doyou recognize them on a new device?For many applications, the answer is the APIs.
With theuser's permission, you can use Account Manager to fetch the account namesthat the user has stored on their device.Integration with the user's accounts allows you to do a variety of things such as:. Auto-fill forms with the user's email address.
Retrieve an ID that is tied to a user, not the device.Determine if AccountManager is for youApplications typically try to remember the user using one of three techniques:. Ask the user to type in a username.
![Manager Manager](https://www.top1apk.com/wp-content/uploads/2018/09/Google-Account-Manager-for-Android-e1537626969504.jpg)
To get apps from the Google Play Store, you'll need an account on your device. Sure that you sign in as the right user before changing account information.
Retrieve a unique device ID to remember the device. Retrieve a built-in account fromOption (a) is problematic. First, asking the user to type something beforeentering your app will automatically make your app less appealing.
Second,there's no guarantee that the username chosen will be unique.Option (b) is less onerous for the user, but it's. Moreimportantly, it only allows you to remember the user on one device. Imagine thefrustration of someone who upgrades to a shiny new device, only to find thatyour app no longer remembers them.Option (c) is the preferred technique.
Account Manager allows you to getinformation about the accounts that are stored on the user's device. As we'llsee in this lesson, using Account Manager lets you remember your user, no matterhow many devices the user may own, by adding just a couple of extra taps to yourUI. Decide what type of account to useAndroid devices can store multiple accounts from many different providers.When you query for account names, you can choose to filterbyaccount type. The account type is a string that uniquely identifies the entitythat issued the account. For instance, Google accounts have type 'com.google,'while Twitter uses 'com.twitter.android.auth.login.'
Request GETACCOUNT permissionIn order to get a list of accounts on the device, your app needs thepermission. Add a tag in your manifest file to requestthis permission.Query AccountManager for a list of accountsOnce you decide what account type you're interested in, you need to query for accounts of thattype. Get an instance of by calling. Then use thatinstance to call. Kotlinval am: AccountManager = AccountManager.get(this) // 'this' references the current Contextval accounts: Array = am.getAccountsByType('com.google')JavaAccountManager am = AccountManager.get(this); // 'this' references the current ContextAccount accounts = am.getAccountsByType('com.google');This returns an array of objects. If there's more than oneinthe array, you should present a dialog asking the user to select one.
Use the account object to personalize your appThe object containsan account name, which for Google accounts is an email address. You can use this information inseveral different ways, including the following:. As suggestions in forms, so the user doesn't need to input account information byhand. As a key into your own online database of usage and personalization information.Decide whether an account name is enoughAn account name is a good way to remember the user, but theobject byitself doesn't protect your data or give you access to anything besides the user's account name. If your appneeds to allow the user to go online to access private data, you'll need something stronger: authentication.The next lesson explains how to authenticate to existing online services.
The lesson after thatdeals with writing a custom authenticator so that you can install your ownaccount types.Content and code samples on this page are subject to the licenses described in the. Java is a registered trademark of Oracle and/or its affiliates.