Getting the Gmail Id of the User In Android 6.0 marshmallow

后端 未结 3 798
遇见更好的自我
遇见更好的自我 2021-01-15 05:50

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

 try {
            Account[] accounts = AccountManager.get(this).getA         


        
3条回答
  •  执念已碎
    2021-01-15 06:32

    You need to add run-time permission for Android 6.0 marshmallow. This is working code.

    // Check my test phone os version that is it marshmallow. Use this inside onCreate method.

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

    // check have you already run-time permission for marshmallow or not.

    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);
        }
    

    // at last check onRequestPermissionsResult @Override method for checking user allow permission or not. if allow then possibleEmail is your mail address.

        @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);
                    }
                }
    
    }
    

提交回复
热议问题