Tony's ramblings on Open Source Software, Life and Photography

Finding Google Account on Multiple Android Versions

Cell phone pile @ CCCBIf you want to develop for the most Android devices, chances are you will run across something you need to do differently depending on the age of the device at hand. Newer API's contain different features from old ones.

There's a slick little trick whereby you can handle multiple API versions easily. For instance, finding the Google email address(es) on the phone is a challenge for anything that bridges the "Eclair" threshold. I finally kludged together from multiple sources a working example - nobody handled it quite correctly to do this from any one example I found - although this one came close.

Create an abstract class that manages the details for you like so:

 

  1.  import android.accounts.Account;
  2.  import android.accounts.AccountManager;
  3.  import android.app.Activity;
  4.  import android.os.Build;
  5.  
  6.  public abstract class GoogleAccountsWrapper {
  7.   public static GoogleAccountsWrapper getInstance() {
  8.   if (Integer.parseInt(Build.VERSION.SDK) < 5)
  9.   {
  10.   return PreEclair.Holder.instance;
  11.   }
  12.   else
  13.   {
  14.   return EclairPlus.Holder.instance;
  15.   }
  16.   }
  17.  
  18.   public abstract String[] initGMailAccount(Activity activity);
  19.  
  20.   private static class PreEclair extends GoogleAccountsWrapper
  21.   {
  22.   private static class Holder {
  23.   private static final PreEclair instance = new PreEclair();
  24.   }
  25.   @Override
  26.   public String[] initGMailAccount(Activity activity) {
  27.   com.google.android.googlelogin.GoogleLoginServiceHelper
  28.   .getAccount(activity, 12345, true);
  29.   return null;
  30.   }
  31.  
  32.   }
  33.  
  34.   private static class EclairPlus extends GoogleAccountsWrapper
  35.   {
  36.   private static class Holder {
  37.   private static final EclairPlus instance = new EclairPlus();
  38.   }
  39.   @Override
  40.   public String[] initGMailAccount(Activity activity) {
  41.   int mycount = 0;
  42.   AccountManager accountManager = AccountManager.get(activity);
  43.   Account[] accounts = accountManager.getAccounts();
  44.   mycount = accounts.length;
  45.   if (mycount < 1) {
  46.   return null;
  47.   }
  48.   String[] mystrings = new String[mycount];
  49.   int i =0;
  50.   for(Account account: accounts) {
  51.   mystrings[i] = account.name;
  52.   i++;
  53.   }
  54.   return mystrings;
  55.   }
  56.  
  57.   }
  58.  
  59.  }

Now, you can do something like this from your app:

  1.  GoogleAccountsWrapper myaccounts = GoogleAccountsWrapper.getInstance();
  2.  String[] accounts = myaccounts.initGMailAccount(this);
  3.  if (accounts != null) {
  4.   ...

And don't forget to override the OnActivityResult and read the accounts from the data provided there, if it happens (the old method.)


Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <p>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for preventing automated spam submissions. It is case sensitive.
Image CAPTCHA
Enter the characters shown in the image.