10

I've been experimenting with the Android SDK over the past few days, in readiness to write an App for the store, however I've run across a bit of a problem.

The App I'll be writing requires that the user has a Google account associated with the phone. Retrieving and making use of the Auth token etc was not a problem, however I would like to be able to show the activity that a user would normal reach by going through the menus Settings->Accounts->Add Account.

Now through experimentation I've been able to launch this activity from the shell using the following command.

am start -n com.google.android.gsf/.login.AccountIntroActivity

I'm having trouble performing the same action in JAVA using the Intent class.

Would anyone be able to tell me firstly whether or not this can be done via JAVA, and secondly how I could go about it please?

If I have to settle for the Sync Settings screen then I will (this can be achieved through the Settings.ACTION_SYNC_SETTINGS intent), however it'd be quite nice to be able to direct the user straight to the required screen.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Seidr
  • 4,946
  • 3
  • 27
  • 39

6 Answers6

15

Check out the ACTION_ADD_ACCOUNT

startActivity(new Intent(Settings.ACTION_ADD_ACCOUNT));
mattlaabs
  • 486
  • 3
  • 14
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • My thanks - that is one step closer to what I'm trying to achieve, however to quote our Jedi masters - 'this is not the Activity you are looking for'. It is the next step in the chain I am trying to invoke - part of the Setup Wizard - the activity that appears when you click on the 'Google' account symbol. If this is impossible, I'll stick with the ACTION_ADD_ACCOUNT answer. Thanks – Seidr Aug 26 '10 at 13:47
  • Probably it's impossible because I don't see on the linked resource. And that is the only page that describes the settings globals. – Pentium10 Aug 26 '10 at 13:50
  • That's what I feared - I imagine access to the component I'm calling above from adb shell (com.google.android.gsf/.login.AccountIntroActivity) is probably restricted some how. – Seidr Aug 26 '10 at 13:53
3

the answer for the above question by providing EXTRA_ACCOUNT_TYPES in the intent extra data. and set the value to "com.google" in order to alert the activity:

public static void startAddGoogleAccountIntent(Context context){
Intent addAccountIntent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
addAccountIntent.putExtra(Settings.EXTRA_ACCOUNT_TYPES, new String[] {"com.google"});
context.startActivity(addAccountIntent); }
SHADOW NET
  • 69
  • 4
3

Try the following:

public static void addGoogleAccount(final Activity activity) {
    final AccountManager accountMgr = AccountManager.get(activity);
    accountMgr.addAccount("com.google", "my_auth_token", null, null, activity, null, null);
}
markjan
  • 685
  • 6
  • 9
  • Thanks markjan - I'll check that out once I've got my SDK setup again. – Seidr Nov 08 '10 at 16:11
  • "my_auth_token" is it a valid auth token type for Google account? If no, then can you please tell me valid auth token type for adding Google account? – Saurabh Jain Mar 22 '16 at 10:50
3

Android Account Manager provides an API to add account. (google or other account types)

public AccountManagerFuture addAccount (String accountType, String authTokenType, String[] requiredFeatures, Bundle addAccountOptions, Activity activity, AccountManagerCallback callback, Handler handler)

http://developer.android.com/reference/android/accounts/AccountManager.html

Sam
  • 31
  • 2
2

For recent Androids using adb you can do:

adb shell am start -a android.settings.ADD_ACCOUNT_SETTINGS \
                   -n com.android.settings/.accounts.AddAccountSettings

(You’ll still have to select what account type you’d like though)

rumpel
  • 7,870
  • 2
  • 38
  • 39
  • Thank you for this suggestion! Is it possible to add/pass the pre-determined values to the Android Account Manager? For example in a `davdroid-config.json` ? – a.t. Jan 06 '23 at 09:21
1

The clue is in your shell command:

    Intent intent = new Intent();
    intent.setClassName( "com.google.android.gsf", "com.google.android.gsf.login.AccountIntroActivity" );
    context.startActivity( intent );
bennemy
  • 11
  • 2