Programmatically starting the 'Add Google Account' activity in Android

8.7k views Asked by At

I am developing an application which needs a Google account for certain options. Options are disabled when no account is detected, but I am presenting the user to add one by asking via a popup, if user clicks yes, the activity should start. It's working fine to display the global "Add account" page, but I want to skip that uncalled for extra step. After all, why present someone with the option to add a Exchange account if a Google account is needed, that's just confusing. So I want to default to the new Google account setup page.

Java

try {
    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.setClassName( "com.google.android.gsf", "com.google.android.gsf.login.AccountIntroActivity");

    //if(getApplicationContext().getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
        getApplicationContext().startActivity(intent);
    //} else {
        //getApplicationContext().startActivity(new Intent(Settings.ACTION_ADD_ACCOUNT).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    //}
} catch ( ActivityNotFoundException e) {
    e.printStackTrace();
}

When I run this, the following exeception is thrown:

05-29 18:24:50.741: W/System.err(10875): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.google.android.gsf/com.google.android.gsf.login.AccountIntroActivity}; have you declared this activity in your AndroidManifest.xml?

Androidmanifest.xml

    <activity 
        android:name="com.google.android.gsf.login.AccountIntroActivity"/>   

QUESTION: what am I missing here?

EDIT:

I tried a different way using addAccount, this doesn't work, nothing happens, no errors are thrown, no new activity starts to add the Google account. By the way, the entire try catch block in the original version is in a AlertDialog/ listener.

AccountManager acm = AccountManager.get();
acm.addAccount("com.google", null, null, null, null, null, null);           
2

There are 2 answers

2
slinden77 On BEST ANSWER

Ok, the problem using the AccountManager way was that the Activity context not being used by me in the method call at all, or not correctly. Given the fact it was used in a DialogInterface, this works:

private void popup() {
     AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
     helpBuilder.setTitle("Add Gmail account");
     helpBuilder.setMessage("These options rely on a Gmail account, but you 
     don't seem to have one configured. Would you like to configure one now?");

     helpBuilder.setPositiveButton("Yes",
     new DialogInterface.OnClickListener() {
         //@Override
         public void onClick(DialogInterface dialog, int which) {
             //try/ catch block was here
             AccountManager acm = AccountManager.get(getApplicationContext());
             acm.addAccount("com.google", null, null, null, thisclassname.this, 
             null, null);
            }
     });

     helpBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
             // close the dialog, return to activity
         }
     });    

     AlertDialog helpDialog = helpBuilder.create();
     helpDialog.show();
}//end method

This probably needs some more work to be able to actually use the configured account name, but for now, this answers the Q.

Sadly, this requires a permission, but I guess that's just how things are

8
Nikolay Elenkov On

You are effectively trying to use a private API -- the class name of the add Google account activity may change, or it may already be different on different Android versions. It is located in one of the Google services packages, and you certainly shouldn't add its name to your manifest. In short, this is a hack, don't do it. Doesn't AccountManager.addAcount("com.google",...) work for you (you need the MANAGE_ACCOUNTS permission)?