在Android 6.0棉花糖中获取用户的Gmail Id

在Android 6.0棉花糖中获取用户的Gmail Id,android,android-6.0-marshmallow,android-account,Android,Android 6.0 Marshmallow,Android Account,我正在使用android.permission.GET_ACCOUNTS获得电子邮件id try { Account[] accounts = AccountManager.get(this).getAccountsByType("com.google"); for (Account account : accounts) { emailid = account.name; Log.

我正在使用android.permission.GET_ACCOUNTS获得电子邮件id

 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);
        }
此代码适用于所有达到棒棒糖5.1的设备。 但它在棉花糖6.0中不起作用

谁能帮我解决这个问题。
我甚至没有在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
是设备的电子邮件

您需要为Android 6.0棉花糖添加运行时权限。这是工作代码

//检查我的测试手机操作系统版本,这是棉花糖。使用此内部onCreate方法

   private static final int REQUEST_GET_ACCOUNT = 112;

  if(android.os.Build.VERSION.SDK_INT > 22){
            if(isGETACCOUNTSAllowed()){
               // do your task 

                getMailAddress();
                return;
            }else{
                requestGET_ACCOUNTSPermission();
            }

        }
//检查您是否已经拥有棉花糖的运行时权限

private boolean isGETACCOUNTSAllowed() {
        //Getting the permission status
        int result = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS);

        //If permission is granted returning true
        if (result == PackageManager.PERMISSION_GRANTED)
            return true;

        //If permission is not granted returning false
        return false;
    }


 //if you don't have the permission then Requesting for permission
   private void requestGET_ACCOUNTSPermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.GET_ACCOUNTS)){


        }

        //And finally ask for the permission
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.GET_ACCOUNTS},REQUEST_GET_ACCOUNT);
    }
//最后检查onRequestPermissionsResult@Override方法以检查用户是否允许权限。如果允许,则possibleEmail是您的邮件地址

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        //Checking the request code of our request
        if(requestCode == REQUEST_GET_ACCOUNT){

            //If permission is granted
            if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){


                Toast.makeText(this,"Thanks You For Permission Granted ",Toast.LENGTH_LONG).show();

           getMailAddress();

            }else{
                //Displaying another toast if permission is not granted
                Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show();
            }
        }

}


public void getMailAddress(){

          String possibleEmail = null;

            Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
            Account[] accounts = AccountManager.get(context).getAccountsByType(
                    "com.google");
            for (Account account : accounts) {
                if (emailPattern.matcher(account.name).matches()) {
                    possibleEmail = account.name;
                    Log.i("MY_EMAIL_count", "" + possibleEmail);
                }
            }

}

示例作为一个单独的服务类,可以通过调用getGMailAccount方法在不同的情况下轻松使用

公共类Gmail帐户服务{

private static final int MIN_SDK_FOR_REQUESTING_GET_ACCOUNTS = 22;
private static final int GET_ACCOUNTS_PERMISSION_REQUEST_CODE = 112;

private Activity activity;

public GMailAccountService(Activity activity) {
    this.activity = activity;
}

public String getGMailAccount() {
    if(android.os.Build.VERSION.SDK_INT > MIN_SDK_FOR_REQUESTING_GET_ACCOUNTS){
        if(!isGetAccountsPermissionAllowed()){
            requestGetAccountsPermission();
            return getGMailAccount();
        }
    }
    return extractAddressFromAccountManager();
}

private boolean isGetAccountsPermissionAllowed() {
    int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.GET_ACCOUNTS);
    if (result == PackageManager.PERMISSION_GRANTED)
        return true;
    return false;
}

private void requestGetAccountsPermission(){
    ActivityCompat.shouldShowRequestPermissionRationale(activity, android.Manifest.permission.GET_ACCOUNTS);
    ActivityCompat.requestPermissions(activity,new String[]{android.Manifest.permission.GET_ACCOUNTS}, GET_ACCOUNTS_PERMISSION_REQUEST_CODE);
}


public String extractAddressFromAccountManager(){

    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8
    Account[] accounts = AccountManager.get(activity).getAccountsByType("com.google");
    for (Account account : accounts) {
        if (emailPattern.matcher(account.name).matches()) {
            return account.name;
        }
    }
    return null;
}

}

我需要运行时权限吗?是的,你需要<代码>未工作。我认为@rubinellikunnathu是正确的。上面这行不行,因为它不是RT。嗨,塔里克,谢谢你的回答。你能不能编辑一下,添加一点关于你的方法在做什么/你在代码块中更改了什么的解释?你好Tim Malone,对不起,我的英语不好,已经更新了代码,请检查。如果你不明白,请告诉我。我将再次解释必须也添加到AndroidManifest.xml中