3

I'm writing an application in which we are asking the users to associate a google account with so that we can auto sync server side data between installations of the app on different devices.

I'm already using

AccountManager accountManager = AccountManager.get(mContext);
Account[] accounts = accountManager.getAccountsByType("com.google");

to pull the list of available user accounts. In the case there is just one we use that, and in the case of more than one account we ask the users to select an account to associate with the application. What I'm having issue with now is a scenario where there are no google accounts on the device. I'm currently using this

context.startActivity(new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT));

to force an add account dialog; but the issue is it gives the user an option of what account type to add, and we would like to avoid confusing them by just forcing them right in to the add Google account option. Is there a way to do this?

3 Answers 3

6

I know this is an old question now, but Android has provided an alternative solution.

Using AccountManager.addAccount() requires the MANAGE_ACCOUNTS permission. I've found that users balk at your app when they see that permission. You can avoid requiring MANAGE_ACCOUNTS if you use the Settings.EXTRA_ACCOUNT_TYPES extra added in API 18: http://developer.android.com/reference/android/provider/Settings.html#EXTRA_ACCOUNT_TYPES

This launches the add Google account flow:

Intent intent = new Intent(Settings.ACTION_ADD_ACCOUNT);
intent.putExtra(Settings.EXTRA_ACCOUNT_TYPES, new String[] {"com.google"});
startActivity(intent);

I've discovered that even though Settings.EXTRA_ACCOUNT_TYPES was added it API 18, it seems to work on all my devices running ICS or later. Sadly, it did not work on Gingerbread. I'm not sure if it can be trusted to work before API 18, but I've found success so far.

1
  • 1
    I think, it is most correct way to call Add Google Account dialog.
    – Nik
    Feb 4, 2014 at 9:09
5

Something like this should do it:

AccountManager accountMgr = AccountManager.get(mContext);
accountMgr.addAccount("com.google", "ah", null, new Bundle(), (Activity) mContext, null, null);

"ah" is the authorization token type.

3
  • You beat me to answering my own question. Jul 29, 2011 at 8:10
  • Is it possible to check if the account of the current app exists before creating it, without using the permission "GET_ACCOUNTS" ? Nov 2, 2015 at 15:15
  • this is very old but what is "ah"?? you said is the token type but can you explain how I would get that? Jan 6, 2016 at 16:06
1

I'm not entirely sure but: http://www.thialfihar.org/projects/android_add_account/

at the bottom there's a code snippet that adds a google account, tho I'm not very sure if this is what you're looking for.

1
  • The code on that link didn't work BUT it did however give me a pointer in the right direction. I'm going to give you an up vote because of that and add the answer I found. Jul 29, 2011 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.