Getting Android owner's email address nicely

前端 未结 4 414
忘了有多久
忘了有多久 2020-11-29 16:47

I want to allow the user to provide me their email address without typing it in. Ideally, there\'d be a text field where the user could either type an email address or push

相关标签:
4条回答
  • 2020-11-29 17:16

    I know I'm way too late, but this might be useful to others.

    I think the best way to auto-populate an email field now is by using AccountPicker

    If your app has the GET_ACCOUNTS permission and there's only one account, you get it right away. If your app doesn't have it, or if there are more than one account, users get a prompt so they can authorize or not the action.

    Your app needs to include the Google Play Services auth library com.google.android.gms:play-services-auth but it doesn't need any permissions.

    This whole process will fail on older versions of Android (2.2+ is required), or if Google Play is not available so you should consider that case.

    Here's a basic code sample:

        private static final int REQUEST_CODE_EMAIL = 1;
        private TextView email = (TextView) findViewById(R.id.email);
    
        // ...
    
        try {
            Intent intent = AccountPicker.newChooseAccountIntent(null, null,
                    new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);
            startActivityForResult(intent, REQUEST_CODE_EMAIL);
        } catch (ActivityNotFoundException e) {
            // TODO
        }
    
        // ...
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
                String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                email.setText(accountName);
            }
        }
    
    0 讨论(0)
  • 2020-11-29 17:19

    An alternative approach might be to find the Google username associated with the device, from which you can reconstruct a user@gmail.com address, but that still requires use of the AccountManager. See Accessing Google Account Id /username via Android.

    0 讨论(0)
  • 2020-11-29 17:29

    Indeed, not only can't you do this without GET_ACCOUNTS, the information you want only exists in the user's (Google) account data.

    On my Sprint phone, the 'owner' address sprint assigns is myusername@sprintpcs.com, and that can be seen using getprop from a shell. But that's not my primary email address, or even one I ever use/check. What you want is my gmail address, and that's stored in the Android account data.

    Actually, you want one of the two — which is another reason you need GET_ACCOUNTS. If you're going to ask for my email address, you need to let me select among the two @gmail.com accounts I have configured on the device. I know which one is my 'preferred' one (although even that's contextual), but AFAIK neither is considered by the system to be more 'primary' or 'default' than the other.

    0 讨论(0)
  • 2020-11-29 17:30

    There is no nice way to access the user's account (e-mail) information without asking for the GET_ACCOUNTS permission. :-)

    0 讨论(0)
提交回复
热议问题