blob: d4a6f7accc2870edb09676a96ffa02f0a55c66ca [file] [log] [blame]
Scott Main98af5edc72011-12-15 20:13:20 -08001page.title=Remembering Your User
Dirk Dougherty3b33cdc2011-12-15 15:34:41 -08002parent.title=Remembering and Authenticating Users
Scott Mainf9cca662011-12-15 10:07:03 -08003parent.link=index.html
4
5trainingnavtop=true
6next.title=Authenticating to OAuth2 Services
7next.link=authenticate.html
8
9@jd:body
10
11
12<div id="tb-wrapper">
13 <div id="tb">
14<h2>This lesson teaches you to</h2>
15<ol>
16 <li><a href="#ForYou">Determine if AccountManager for You</a></li>
17 <li><a href="#TaskTwo">Decide What Type of Account to Use</a></li>
18 <li><a href="#GetPermission">Request GET_ACCOUNT permission</a></li>
19 <li><a href="#TaskFive">Query AccountManager for a List of Accounts</a></li>
Dirk Dougherty3b33cdc2011-12-15 15:34:41 -080020 <li><a href="#IdentifyUser">Use the Account Object to Personalize Your App</a></li>
21 <li><a href="#IdIsEnough">Decide Whether an Account Name is Enough</a></li>
Scott Mainf9cca662011-12-15 10:07:03 -080022</ol>
23 </div>
24</div>
25
26
27<p>Everyone likes it when you remember their name. One of the simplest, most
28effective things you can do to make your app more lovable is to remember who
29your user is&mdash;especially when the user upgrades to a new device or starts carrying
30a tablet as well as a phone. But how do you know who your user is? And how do
31you recognize them on a new device?</p>
32
33<p>For many applications, the answer is the {@link android.accounts.AccountManager} APIs. With the
Dirk Dougherty3b33cdc2011-12-15 15:34:41 -080034user's permission, you can use Account Manager to fetch the account names
35that the user has stored on their device.</p>
Scott Mainf9cca662011-12-15 10:07:03 -080036
37<p>Integration with the user's accounts allows you to do a variety of things such as:</p>
38<ul>
39<li>Auto-fill forms with the user's email address.</li>
40<li>Retrieve an ID that is tied to a user, not the device.</li>
41</ul>
42
43
44<h2 id="ForYou">Determine if AccountManager for You</h2>
45
Dirk Dougherty3b33cdc2011-12-15 15:34:41 -080046<p>Applications typically try to remember the user using one of three techniques:</p>
Scott Mainf9cca662011-12-15 10:07:03 -080047<ol type="a">
48<li>Ask the user to type in a username </li>
Dirk Dougherty3b33cdc2011-12-15 15:34:41 -080049<li>Retrieve a unique device ID to remember the device</li>
Scott Mainf9cca662011-12-15 10:07:03 -080050<li>Retrieve a built-in account from {@link android.accounts.AccountManager}</li>
51</ol>
52
53<p>Option (a) is problematic. First, asking the user to type something before
54entering your app will automatically make your app less appealing. Second,
55there's no guarantee that the username chosen will be unique. </p>
56
57<p>Option (b) is less onerous for the user, but it's
58<a href="http://android-developers.blogspot.com/2011/03/identifying-app-installations.html">tricky
59to get right</a>. More
60importantly, it only allows you to remember the user on one device. Imagine the
61frustration of someone who upgrades to a shiny new device, only to find that
62your app no longer remembers them.</p>
63
64<p>Option (c) is the preferred technique. Account Manager allows you to get
65information about the accounts that are stored on the user's device. As we'll
Dirk Dougherty3b33cdc2011-12-15 15:34:41 -080066see in this lesson, using Account Manager lets you remember your user, no matter
Scott Mainf9cca662011-12-15 10:07:03 -080067how many devices the user may own, by adding just a couple of extra taps to your
68UI.</p>
69
70
71<h2 id="TaskTwo">Decide What Type of Account to Use</h2>
72
73<p>Android devices can store multiple accounts from many different providers.
74When you query {@link android.accounts.AccountManager} for account names, you can choose to filter
75by
76account type. The account type is a string that uniquely identifies the entity
77that issued the account. For instance, Google accounts have type "com.google,"
78while Twitter uses "com.twitter.android.auth.login."</p>
79
80
81<h2 id="GetPermission">Request GET_ACCOUNT permission</h2>
82
83<p>In order to get a list of accounts on the device, your app needs the {@link
84android.Manifest.permission#GET_ACCOUNTS}
85permission. Add a <a href="{@docRoot}guide/topics/manifest/uses-permission-element.html">{@code
86&lt;uses-permission&gt;}</a> tag in your manifest file to request
87this permission:</p>
88
89<pre>
90&lt;manifest ... >
91 &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
92 ...
93&lt;/manifest>
94</pre>
95
96
97<h2 id="TaskFive">Query AccountManager for a List of Accounts</h2>
98
99<p>Once you decide what account type you're interested in, you need to query for accounts of that
100type. Get an instance of {@link android.accounts.AccountManager} by calling {@link
101android.accounts.AccountManager#get(android.content.Context) AccountManager.get()}. Then use that
102instance to call {@link android.accounts.AccountManager#getAccountsByType(java.lang.String)
103getAccountsByType()}.</p>
104
105<pre>
106AccountManager am = AccountManager.get(this); // "this" references the current Context
107
108Account[] accounts = am.getAccountsByType("com.google");
109</pre>
110
111<p>This returns an array of {@link android.accounts.Account} objects. If there's more than one
112{@link android.accounts.Account} in
113the array, you should present a dialog asking the user to select one.</p>
114
115
Dirk Dougherty3b33cdc2011-12-15 15:34:41 -0800116<h2 id="IdentifyUser">Use the Account Object to Personalize Your App</h2>
Scott Mainf9cca662011-12-15 10:07:03 -0800117
118<p>The {@link android.accounts.Account} object contains an account name, which for Google accounts
119is an
120email address. You can use this information in several different ways, such as:
121<ul>
122 <li> As suggestions in forms, so the user doesn't need to input account information by
123hand.</li>
124 <li> As a key into your own online database of usage and personalization information.</li>
125</ul>
126</p>
127
128
Dirk Dougherty3b33cdc2011-12-15 15:34:41 -0800129<h2 id="IdIsEnough">Decide Whether an Account Name is Enough</h2>
Scott Mainf9cca662011-12-15 10:07:03 -0800130
Dirk Dougherty3b33cdc2011-12-15 15:34:41 -0800131<p>An account name is a good way to remember the user, but the {@link android.accounts.Account}
Scott Mainf9cca662011-12-15 10:07:03 -0800132object by
Dirk Dougherty3b33cdc2011-12-15 15:34:41 -0800133itself doesn't protect your data or give you access to anything besides the user's account name. If your app
134needs to allow the user to go online to access private data, you'll need something stronger: authentication.
Scott Mainf9cca662011-12-15 10:07:03 -0800135The next lesson explains how to authenticate to existing online services. The lesson after that
136deals with writing a custom authenticator so that you can install your own
137account types.</p>