How to get the Android device's primary e-mail address

后端 未结 12 2308
借酒劲吻你
借酒劲吻你 2020-11-21 13:30

How do you get the Android\'s primary e-mail address (or a list of e-mail addresses)?

It\'s my understanding that on OS 2.0+ there\'s support for multiple e-mail add

12条回答
  •  礼貌的吻别
    2020-11-21 13:46

    Working In MarshMallow Operating System

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

提交回复
热议问题