How to get user’s e-mail without explicitly asking for permission?

Sorry to disappoint you – it’s not a hack.

Before Android 4.0, the official way was to use the AccountManager class which required the android.permission.GET_ACCOUNTS permission.

Later in API 14, a new method was introduced, which relies on ContactsContract.Profile. There’s been some controversy around it, as it requires two extremely sensitive permissions: android.permission.READ_PROFILE and android.permission.READ_CONTACTS.

Luckily, in most cases, you can bypass all these permissions and use the AccountPicker:

Intent emailIntent = AccountPicker.newChooseAccountIntent(null, null,
                new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);
startActivityForResult(emailIntent, REQUEST_CODE_EMAIL);

This will prompt user to choose his account and retrieve the e-mail based on his choice. No scary permission dialog, no suspicions.

The second and the last step is to get the e-mail in onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
        String email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *