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?
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.
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
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.
Fortunately, for certain account types (including com.google ), the account names are email addresses. Example snippet below.
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.
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" />
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With