Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you get an Android user's email address?

Tags:

java

android

I have android.permission.READ_OWNER_DATA but I can't find any reliable code that would explain how can I read email address of device's owner. How can I do this?

like image 289
nikib3ro Avatar asked Mar 31 '10 21:03

nikib3ro


People also ask

How do I register all email accounts on Android?

For accessing the registered accounts in your Android phone, you must add android. permission. GET_ACCOUNTS permission to your Manifest file. This permission allows access to the list of accounts in the Accounts Service.

How to get the configured email account address programmatically in Android?

Android: How to get the configured email account address programmatically - Stack Overflow I used the below code to get the configured account name Account[] accounts = AccountManager.get(this).getAccounts(); for (Account account : accounts) { Log.d("Account", "Name " + Stack Overflow

How do I get the account name of a device?

You can use AccountManager.getAccounts or AccountManager.getAccountsByType to get a list of all account names on the device. Fortunately, for certain account types (including com.google ), the account names are email addresses. Example snippet below.

Is the name of an account an email address?

Fortunately, for certain account types (including com.google ), the account names are email addresses. Example snippet below.

Is it possible to have multiple e-mail addresses per device?

It's my understanding that on OS 2.0+ there's support for multiple e-mail addresses, but below 2.0 you can only have one e-mail address per device. Show activity on this post. There are several ways to do this, shown below. As a friendly warning, be careful and up-front to the user when dealing with account, profile, and contact data.


2 Answers

Why you wanna do that?

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 your AnroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
like image 142
Jim Blackler Avatar answered Oct 07 '22 22:10

Jim Blackler


please add this permission in your manifest file.

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

Add this function in your MainActivity.java

    public void getAccounts() {          StringBuilder emailAccounts = new StringBuilder();            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {              if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.GET_ACCOUNTS)) {                  ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, 1);              } else {                  ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, 1);              }          }          else {              List<String> accountList = new ArrayList<String>();              Pattern gmailPattern = Patterns.EMAIL_ADDRESS;              Account[] accounts = AccountManager.get(this).getAccounts();              for (Account account : accounts) {                  if (gmailPattern.matcher(account.name).matches()) {                      emailAccounts.append(flag + ". " + "<b>" + account.name + "<br>" +                              "---------" + "---------<br>"); ;                  }              }              deviceDetails.setText(Html.fromHtml(emailAccounts+""));          }      }

now call this function in your onCreate() method like this.

getAccounts();

like image 21
Zia Avatar answered Oct 07 '22 20:10

Zia