Android : how to get the gender and age of the user?

后端 未结 2 642
名媛妹妹
名媛妹妹 2020-12-16 17:07

I have got an Android app that I want to auto-setup according the gender and age of the user.

What are the different ways to get the age and the gender of a user ? (

相关标签:
2条回答
  • 2020-12-16 17:32

    You should use the interface Person, you'll have everything you need to know about the user. (through getGender() and getBirthday() (Or getAgeRange())

    Edit : For using for example let's say getGender(), you would do something around this :

     GoogleApiClient client = new GoogleApiClient.Builder(this)
             .addApi(Plus.API)
             .addScope(Plus.SCOPE_PLUS_LOGIN)
             .setAccountName("users.account.name@gmail.com")
             .build();
    
    
    client.connect();
    Person.Gender gender;
    Person personProfile = Plus.PeopleApi.getCurrentPerson(client);
    
    if (person.hasGender()) // it's not guaranteed
              gender = person.getGender();
    
    0 讨论(0)
  • 2020-12-16 17:39

    An alternative, perhaps more reliable, approach to guessing your user's gender in Android would be hitting a third party API with the email address of your user, then letting the API look for a first name inside the email string, match it against a database of names, then return to you a gender + confidence level. I used "Gender API" (no affiliation).

    Just have to add to AndroidManifest:

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

    Then get your user's Gmail addresses:

    Pattern pattern = Patterns.EMAIL_ADDRESS; 
    Account[] accounts = AccountManager.get(context).getAccounts();
    for (Account account : accounts) {
    if (pattern.matcher(account.name).matches()) {
    
         String emailAddress = account.name
       // grab each of your user's email addresses
    
    
     }
    }
    

    Then, if you want to use the API I used, get a key from gender-api.com and hit this link for each email address:

    https://gender-api.com/get?email=ANDROID_USER_EMAIL_ADDRESS&key=<YOUR_PRIVATE_KEY>
    

    This API returns gender and level of confidence and I'm sure there are others out there that do the same thing.

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