Skip to content

Instantly share code, notes, and snippets.

@richardrussell
Last active December 14, 2015 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardrussell/5032196 to your computer and use it in GitHub Desktop.
Save richardrussell/5032196 to your computer and use it in GitHub Desktop.
Easy way to get user to select and email address (or take default) from Google Accounts available on an Android device. See blog post here: http://www.entrepreneurskitchen.com/2013/02/getting-user-account-on-android.html
import com.google.android.gms.common.AccountPicker;
import android.accounts.AccountManager;
public class MyActivity extends Activity {
// a number to distinguish this startActivityForResult from any others
private static final int GET_GOOGLE_ACCOUNT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate view, etc goes here
// Call the AccountPicker Intent.
// Change the "false" to "true" if you want the user to always be shown the
// Activity, even if there's only one option (this allows them to add an
// Account at the same time, too)
Intent accountIntent = AccountPicker.newChooseAccountIntent(null, null,
new String[] { "com.google" }, false, null, null, null, null);
startActivityForResult(accountIntent, GET_GOOGLE_ACCOUNT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GET_GOOGLE_ACCOUNT && resultCode == RESULT_OK) {
doSomethingWithEmail(data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME));
}
}
// rest of Activity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment