Programmatically starting the 'Add Google Account' activity in Android

slinden77 picture slinden77 · May 29, 2012 · Viewed 8.3k times · Source

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);           

Answer

slinden77 picture slinden77 · May 31, 2012

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