开发者

How can you get an Android user's email address?

I have android.permission.READ_OWNER_DATA but I can't find any reliable code that would explain how can I read email addres开发者_如何学JAVAs of device's owner. How can I do this?


Why you wanna do that?

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

/**
 * This class uses the AccountManager to get the primary email address of the
 * current user.
 */
public class UserEmailFetcher {

  static String getEmail(Context context) {
    AccountManager accountManager = AccountManager.get(context); 
    Account account = getAccount(accountManager);

    if (account == null) {
      return null;
    } else {
      return account.name;
    }
  }

  private static Account getAccount(AccountManager accountManager) {
    Account[] accounts = accountManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0) {
      account = accounts[0];      
    } else {
      account = null;
    }
    return account;
  }
}

In your AnroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS" />


please add this permission in your manifest file.

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

Add this function in your MainActivity.java

    public void getAccounts() {
        StringBuilder emailAccounts = new StringBuilder();

        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.GET_ACCOUNTS)) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, 1);
            } else {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, 1);
            }
        }
        else {
            List<String> accountList = new ArrayList<String>();
            Pattern gmailPattern = Patterns.EMAIL_ADDRESS;
            Account[] accounts = AccountManager.get(this).getAccounts();
            for (Account account : accounts) {
                if (gmailPattern.matcher(account.name).matches()) {
                    emailAccounts.append(flag + ". " + "<b>" + account.name + "<br>" +
                            "---------" + "---------<br>"); ;
                }
            }
            deviceDetails.setText(Html.fromHtml(emailAccounts+""));
        }
    }

now call this function in your onCreate() method like this.

getAccounts();


Working on MarshMallow Version

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


I found the solution

startActivityForResult(intent, ANY_CODE);

@Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ANY_CODE&& resultCode == RESULT_OK) {
            String emailAddress = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            // do something...
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜