Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ? (which are Google Play policy compliant)

For example, is there a way to get these information through Google Play Services ?

Thanks.

like image 848
Regis_AG Avatar asked Apr 16 '15 15:04

Regis_AG


2 Answers

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("[email protected]")
         .build();


client.connect();
Person.Gender gender;
Person personProfile = Plus.PeopleApi.getCurrentPerson(client);

if (person.hasGender()) // it's not guaranteed
          gender = person.getGender();
like image 98
Mekap Avatar answered Oct 17 '22 23:10

Mekap


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.

like image 3
Japes Avatar answered Oct 17 '22 23:10

Japes