3

I am preparing a login page for my app. I wanted to do the same thing that twitter is doing. They are helping the user and predefining some fields like email, name and profile pic. I managed to get the email using the GET_ACCOUNTS permission.

However I cant see how I get the full name and profile pic?

    public static String getEmail(Context context)
{
    AccountManager accountManager = AccountManager.get(context);
    Account account = getAccount(accountManager);
    if (account == null)
    {
        return null;
    }
    else
    {
        return account.name;


    }
}

private static Account getAccount(AccountManager accountManager)
{
    Account[] accounts = accountManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0)
    {
        account = accounts[0];
    }
    else
    {
        account = null;
    }
    return account;
}
user1163234
  • 2,407
  • 6
  • 35
  • 63
  • Perhaps using google-sign in / `GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);`? + user has the ability to select the Google account (if multiple linked). https://developers.google.com/identity/sign-in/android/sign-in – O-9 Aug 29 '19 at 12:41

2 Answers2

1

Get Account Full Name

First Add this permission into AndroidManifest.xml file

 <uses-permission
 android:name="android.permission.GET_ACCOUNTS"/>

Java Code

 public static Account getAccount(AccountManager accountManager) {
        Account[] accounts = accountManager.getAccountsByType("com.google");
        Account account;
        if (accounts.length > 0) {
            account = accounts[0];
        } else {
            account = null;
        }
        return account;
    }

Call getAccount();

    Account account = getAccount(AccountManager.get(context));
    String accountName = account.name;
    String fullName = accountName.substring(0,accountName.lastIndexOf("@"));
    emailEditText.setText(accountName);
    fullNameEditText.setText(fullName);

If Account Email is -

abc@gmail.com

then Name is -

abc

Mansukh Ahir
  • 3,373
  • 5
  • 40
  • 66
  • 2
    What I would like to get is the REAL name of the person. If the e-mail address is "Peter Merchant" , I would like to get "Peter Merchant", and NOT "pmerch". – Holger Jakobs Feb 21 '16 at 19:47
  • @HolgerJakobs you need to Integrating Google Sign-In into Your Android App Ref. https://developers.google.com/identity/sign-in/android/start-integrating – Mansukh Ahir Feb 22 '16 at 10:12
  • No, I don't want to make anything like this a precondition for my app. Just in the likely case there is a (primary) Google account on the machine, I would like to get the proper name of the person. Even if it's a fake name, I wouldn't mind. – Holger Jakobs Feb 22 '16 at 17:14
  • 1
    @HolgerJakobs get name from owner profile check this link http://stackoverflow.com/a/20361088/4395114 – Mansukh Ahir Feb 23 '16 at 05:02
  • Thanks, but asking for permission to read *all* Contacts makes an app very suspicious. I only want to read the (probably fake) name of the owner, which is a huge difference. I do think that the permission system of Android is broken here. – Holger Jakobs Feb 23 '16 at 13:07
-1

Place below code in your activity. You will get what you wanted. This is new way to fetch google user info.

//Global variables:
        GoogleSignInOptions gso;
        private GoogleApiClient mGoogleApiClient;    
    //inside onCreate():
        gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                            .requestEmail()
                            .requestProfile()
                            .requestScopes(new Scope(Scopes.PLUS_ME))
                            .requestScopes(new Scope(Scopes.PLUS_LOGIN))
                            .requestScopes(new Scope(Scopes.PROFILE))
                            .build();
                    mGoogleApiClient = new GoogleApiClient.Builder(this)
                            .enableAutoManage(this , this )
                            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                            .addApi(Plus.API)
                            .build();

Place Below code out of onCreate()

 //to get google account info:
    private void handleSignInResult(GoogleSignInResult result) {
            Log.d(TAG, "handleSignInResult:" + result.isSuccess());
            if (result.isSuccess()) {
                GoogleSignInAccount acct = result.getSignInAccount();
                String pic_info=null;
                int g;
                String gender="Null";
                String userid="";
                if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                    Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                    userid=currentPerson.getId(); //BY THIS CODE YOU CAN GET CURRENT LOGIN USER ID
                    g=currentPerson.getGender();
                    gender = (g==1)?"Female":(g==0)?"Male":"Others";
                }

                if(acct.getPhotoUrl() != null) {
                    pic_info = acct.getPhotoUrl().toString();
                    Log.e("info", pic_info + " ");
                }
                Toast.makeText(getApplicationContext(),"welcome "+acct.getDisplayName(),Toast.LENGTH_LONG).show();
                SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("user", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("social_media","google");
                editor.putString("id",userid);
                editor.putString("email",acct.getEmail());
                editor.putString("name",acct.getDisplayName());
                editor.putString("profile_pic",pic_info);
                editor.putString("gender",gender);
                editor.apply();
                Intent intent=new Intent(LoginActivity.this,SignOutActivity.class);
                startActivity(intent);
                finish();
            }
Manav Patadia
  • 848
  • 7
  • 12
  • Please format and explain your code adequately so that your answer is as helpful as possible for the OP and future readers. Simple code dumps make for hard-to-understand answers – avalancha Jun 22 '16 at 13:05