在Android 6.0棉花糖中获取用户的Gmail ID [英] Getting the Gmail Id of the User In Android 6.0 marshmallow

查看:113
本文介绍了在Android 6.0棉花糖中获取用户的Gmail ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android.permission.GET_ACCOUNTS权限获取电子邮件ID.

I am getting email id by using android.permission.GET_ACCOUNTS permission.

 try {
            Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");
            for (Account account : accounts) {
                emailid = account.name;
                Log.e("account",emailid);
            }
        } catch (Exception e) {
            Log.i("Exception", "Exception:" + e);
        }

此代码可在Lollipop 5.1之前的所有设备上运行. 但是它在棉花糖6.0中不起作用.

This code is working on all devices upto Lollipop 5.1. But its not working in Marshmallow 6.0.

任何人都可以帮助我解决此问题. 我什至在logcat中都没有收到任何错误.

Can anyone help me to resolve this problem. I am not even getting any errors in logcat.

推荐答案

此代码有效,在Android 4.4.4、5.0.1、6.0和6.0.1上进行了测试

String possibleEmail = "";
    final Account[] accounts = AccountManager.get(context).getAccounts();
    //Log.e("accounts","->"+accounts.length);
    for (Account account : accounts) {
        if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) {
            possibleEmail = account.name;
        }
    }

possibleEmail是设备的电子邮件.

possibleEmail is the email of the device.

这篇关于在Android 6.0棉花糖中获取用户的Gmail ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆