Sometimes we need to get the Google account name bound to the current mobile phone, that is, the Gmail account name, to mark different users.
Now I have found that there are three ways to get a Gmail account:
1 Android built-in API
This method is the simplest and easiest to implement. However, this method requires a get_accounts user permission. In stackoverflow, this permission is a heavy duty permission. this is because not only the Gmail account on the device, but other accounts can be exposed to the program. Sometimes we only need the Gmail account. However, I have observed that this API cannot identify the primary account when the device is associated with two Gmail accounts at the same time.
2 Google Play Service
This method avoids the permission mentioned above, but there is also a small problem in this method, that is, the method he obtains is to initiate an intent, which will create a dialog, then let you select an account and get the Gmail account in this way
3 Google +
This method is actually a continuation of the second method, and requires the permissions of the first method, the advantage of this method is that you can not only obtain the user's Gmail, but also obtain other information such as the user's profile picture and nickname on Google + U. In this article, we will not focus on this method, but just attach a link. The link is very detailed and contains the source code (I will also attach it ).
The first two implementation methods are described below:
1. Method 1:
Public static string getemail (context) {accountmanager = accountmanager. get (context); account [] accounts = accountmanager. getaccountsbytype ("com. google "); // get the Google account Account account = accounts. length> 0? Accounts [0]: NULL; // obtain the first account return account = NULL? Null: account. Name ;}
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
2. method 2
This method requires a library of Google Play services, which can be downloaded in SDK manager. However, due to the impact of Great Wall, it is difficult to download, it is best to find an offline file and put it directly under the android SDK. The main code logic is as follows:
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext())) { try { Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null); startActivityForResult(intent, REQUEST_CODE_EMAIL); } catch (ActivityNotFoundException e) { } } } activity result: private static final int REQUEST_CODE_EMAIL = 1; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) { String gmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); } }
3. The last method is slightly complicated:
Here we reference an article by an Indian developer: The androidhive article is easy to understand, and the document quality is also good, with diagrams, code, and source code.
Open-source China does not seem to be able to upload attachments. You need to leave a mailbox :)