Get Gmail ID for Android user

I do not know if this is the right question or not. How can we get the user ID from which he was sent to the Google Play Store in Android. It is possible or not.

+4
source share
2 answers

As far as I know, the user must set up his gmail account on his Android phone, and then he gets access to Google Play.

You can get account information as follows (from Jim Blackler ):

import android.accounts.Account; import android.accounts.AccountManager; import android.content.Context; /** * This class uses the AccountManager to get the primary email address of the * current user. */ public class UserEmailFetcher { 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; } } 

In manifest

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

If you use a method that does not require a permission request through AndroidManifest.xml , you can use the Google Play AccountPicker services .

Create a dialog that allows the user to select the desired Google account:

 Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"}, false, null, null, null, null); startActivityForResult(intent, SOME_REQUEST_CODE); 

then process the result in Activity :

 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { if (requestCode == SOME_REQUEST_CODE && resultCode == RESULT_OK) { String emailAddress = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); // do something... } } 

Remember to add compile 'com.google.android.gms:play-services-identity:8.+' to the dependencies in your build.gradle file (check here for the most recent version number used).

+4
source

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


All Articles