Programmatically Launch Add Google Account Activity on Android

I am developing an application that requires a Google account for certain parameters. The options are disabled when the account is not found, but I suggest the user to add it, requesting through a pop-up window, if the user clicks "yes", the action should begin. It works great to display the global "Add Account" page, but I want to skip this unclaimed for an extra step. In the end, why does anyone have the option to add an Exchange account if they need a Google account that is just confusing. Therefore, I want to use the new Google Account Settings page by default.

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 ran this, the following exeception is thrown:

05-29 18: 24: 50.741: W / System.err (10875): android.content.ActivityNotFoundException: Cannot find explicit activity class {com.google.android.gsf / com.google.android.gsf. login.AccountIntroActivity}; Have you announced 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 using addAccount in a different way, it doesnโ€™t work, nothing happens, no errors occur, no new activity starts adding a Google account. By the way, the entire catch try block in the original version is in AlertDialog / listener.

 AccountManager acm = AccountManager.get(); acm.addAccount("com.google", null, null, null, null, null, null); 
+6
source share
2 answers

Well, the problem with using the AccountManager path was that the Activity context is not used by me in the method call at all or incorrectly. Given the fact that it was used in 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 will probably require another work in order to be able to use the configured account name, but so far it is responding to Q.

Unfortunately, permission is required for this, but I guess that's just it

+6
source

In fact, you are trying to use a private API - the class name for adding activity to your Google account may change or, perhaps, it differs from different versions of Android. It is located in one of the Google service packages, and of course you should not add its name to your manifest. In short, itโ€™s a hack, donโ€™t do it. Does AccountManager.addAcount("com.google",...) for you (do you need MANAGE_ACCOUNTS permission)?

+4
source

Source: https://habr.com/ru/post/916929/


All Articles