你怎么能得到一个Android用户的电子邮件地址?

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

你为什么想这样做?

 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" /> 

在MarshMallow版本上工作

btn_click =(Button)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); } } });