Android: get user ID without requiring scary (for user) permissions?

后端 未结 4 1322
天涯浪人
天涯浪人 2021-02-04 04:33

In order to manage user preferences, at present I\'m grabbing the google user name (effectively the email address they\'ve registered to the device) and using (a hash of) that a

相关标签:
4条回答
  • 2021-02-04 05:14

    If you use Google Play Services you can get the account type and account name without any extra permissions.

    First, add the following dependency to build.gradle:

    compile 'com.google.android.gms:play-services-auth:8.4.0'
    

    Next, launch the account chooser intent:

    Intent intent = AccountPicker.newChooseAccountIntent(null, null,
        new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE},
        false, null, null, null, null);
    
    try {
      startActivityForResult(intent, REQUEST_CODE_EMAIL);
    } catch (ActivityNotFoundException e) {
      // This device may not have Google Play Services installed.
      // TODO: do something else
    }
    

    Finally, override onActivityResult to get the account type and account name:

    @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);
        String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
        // TODO: do something with the accountName
        return;
      }
      super.onActivityResult(requestCode, resultCode, data);
    }
    

    source: https://stackoverflow.com/a/19444640/1048340

    0 讨论(0)
  • 2021-02-04 05:20

    You can get the exact gmail email address using AccountPicker without requiring to add the permissions.

    Your app needs to include the Google Play Services 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.

    Sample code from the source:

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

    Source of the above information is : This Answer on SO - to a similar but slightly different question asked in the past.

    This Post - also has some nice approaches that would serve your purpose.

    0 讨论(0)
  • 2021-02-04 05:30

    You can use a Google advertising ID as a UUID, but it's not cross-device(yet?):

    https://support.google.com/googleplay/android-developer/answer/6048248?hl=en

    That'll at least get rid of the scary permission request.

    I don't believe Android/Google has a cross-device UUID that doesn't require permission, though Doubleclick by Google supposedly was developing one.

    0 讨论(0)
  • 2021-02-04 05:33

    Unfortunately there is no solution to do that, even the most basic account access requires user permission.

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