在Android 6.0 marshmallow中获取用户的Gmail ID

Mr *_*bot 2 android android-account android-6.0-marshmallow

我通过使用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);
        }
Run Code Online (Sandbox Code Playgroud)

此代码适用于Lollipop 5.1之前的所有设备.但它不适用于Marshmallow 6.0.

任何人都可以帮我解决这个问题.我甚至没有在logcat中收到任何错误.

Tar*_*qul 12

您需要为Android 6.0 marshmallow添加运行时权限.这是工作代码.

//检查我的测试手机操作系统版本是棉花糖.在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();
            }

        }
Run Code Online (Sandbox Code Playgroud)

//检查你是否已经获得了marshmallow的运行时权限.

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);
    }
Run Code Online (Sandbox Code Playgroud)

//最后检查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);
                }
            }

}
Run Code Online (Sandbox Code Playgroud)