如何获得Android的主电子邮件地址(或电子邮件地址列表)?
据我所知,OS 2.0+支持多个电子邮件地址,但在2.0以下,每台设备只能有一个电子邮件地址。
如何获得Android的主电子邮件地址(或电子邮件地址列表)?
据我所知,OS 2.0+支持多个电子邮件地址,但在2.0以下,每台设备只能有一个电子邮件地址。
当前回答
Android 8及以上-
步骤1 -在“AndroidManifest.xml -”中添加如下代码
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
步骤2 -在运行时请求权限的活动中添加以下代码。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED) && (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED)) {
getGoogleAccounts();
}
else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.GET_ACCOUNTS, Manifest.permission.READ_CONTACTS}, 1);
//return false;
}
}
步骤3 -为onRequestPermissionsResult -添加代码
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
getGoogleAccounts();
}
}
步骤4 -最后添加代码以使用AccountManager检索帐户
private void getGoogleAccounts(){
AccountManager am = AccountManager.get(this); // "this" references the current Context
Account[] accounts = am.getAccountsByType("com.google");
for (Account acc : accounts){
System.out.println("http accounts " + acc);
}
}
请参考以下链接的变化在安卓8 - https://developer.android.com/about/versions/oreo/android-8.0-changes#aaad
其他回答
使用这个方法:
public String getUserEmail() {
AccountManager manager = AccountManager.get(App.getInstance());
Account[] accounts = manager.getAccountsByType("com.google");
List<String> possibleEmails = new LinkedList<>();
for (Account account : accounts) {
possibleEmails.add(account.name);
}
if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
return possibleEmails.get(0);
}
return "";
}
注意,这需要GET_ACCOUNTS权限:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
然后:
editTextEmailAddress.setText(getUserEmail());
在棉花糖操作系统中工作
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 {
// Lower then Marshmallow
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);
}
}
});
有一个Android api,允许用户选择他们的电子邮件地址,而不需要许可。来看看:https://developers.google.com/identity/smartlock-passwords/android/retrieve-hints
HintRequest hintRequest = new HintRequest.Builder()
.setHintPickerConfig(new CredentialPickerConfig.Builder()
.setShowCancelButton(true)
.build())
.setEmailAddressIdentifierSupported(true)
.setAccountTypes(IdentityProviders.GOOGLE)
.build();
PendingIntent intent = mCredentialsClient.getHintPickerIntent(hintRequest);
try {
startIntentSenderForResult(intent.getIntentSender(), RC_HINT, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Could not start hint picker Intent", e);
}
这将显示一个选择器,用户可以在其中选择电子邮件地址。结果将在onActivityResult()中传递
我会使用Android在ICS中引入的AccountPicker。
Intent googlePicker = AccountPicker.newChooseAccountIntent(null, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null);
startActivityForResult(googlePicker, REQUEST_CODE);
然后等待结果:
protected void onActivityResult(final int requestCode, final int resultCode,
final Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
}
}
Android 8及以上-
步骤1 -在“AndroidManifest.xml -”中添加如下代码
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
步骤2 -在运行时请求权限的活动中添加以下代码。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED) && (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED)) {
getGoogleAccounts();
}
else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.GET_ACCOUNTS, Manifest.permission.READ_CONTACTS}, 1);
//return false;
}
}
步骤3 -为onRequestPermissionsResult -添加代码
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
getGoogleAccounts();
}
}
步骤4 -最后添加代码以使用AccountManager检索帐户
private void getGoogleAccounts(){
AccountManager am = AccountManager.get(this); // "this" references the current Context
Account[] accounts = am.getAccountsByType("com.google");
for (Account acc : accounts){
System.out.println("http accounts " + acc);
}
}
请参考以下链接的变化在安卓8 - https://developer.android.com/about/versions/oreo/android-8.0-changes#aaad