如何获得Android用户的电子邮件地址?

时间:2010-03-31 21:11:29

标签: java android

我有android.permission.READ_OWNER_DATA但我找不到任何可靠的代码来解释如何阅读设备所有者的电子邮件地址。我怎么能这样做?

4 个答案:

答案 0 :(得分:92)

你为什么要这样做?

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;
  }
}

在AnroidManifest.xml中

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

答案 1 :(得分:2)

请在您的清单文件中添加此权限。

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

在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+""));
        }
    }

现在像这样在您的onCreate()方法中调用此函数。

getAccounts();

答案 2 :(得分:0)

使用MarshMallow版本

<强>

btn_click =(按钮)findViewById(R.id.btn_click);

btn_click.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            int permissionCheck = ContextCompat.checkSelfPermission(PermissionActivity.this,
                    android.Manifest.permission.CAMERA);
            if (permissionCheck == PackageManager.PERMISSION_GRANTED)
            {
                //showing dialog to select image
                String possibleEmail=null;

                 Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
                 Account[] accounts = AccountManager.get(PermissionActivity.this).getAccounts();
                 for (Account account : accounts) {
                     if (emailPattern.matcher(account.name).matches()) {
                         possibleEmail = account.name;
                         Log.e("keshav","possibleEmail"+possibleEmail);
                     }
                 }

                Log.e("keshav","possibleEmail gjhh->"+possibleEmail);
                Log.e("permission", "granted Marshmallow O/S");

            } else {                        ActivityCompat.requestPermissions(PermissionActivity.this,
                        new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE,
                                android.Manifest.permission.READ_PHONE_STATE,
                                Manifest.permission.GET_ACCOUNTS,
                                android.Manifest.permission.CAMERA}, 1);
            }
        } else {

//然后降低棉花糖

                String possibleEmail=null;

                 Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
                 Account[] accounts = AccountManager.get(PermissionActivity.this).getAccounts();
                 for (Account account : accounts) {
                     if (emailPattern.matcher(account.name).matches()) {
                         possibleEmail = account.name;
                         Log.e("keshav","possibleEmail"+possibleEmail);
                 }
                Log.e("keshav","possibleEmail gjhh->"+possibleEmail);
        }
    }
});

<强>

答案 3 :(得分:-1)

我找到了解决方案

startActivityForResult(intent, ANY_CODE);

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