2015-04-16 34 views
9

我有一个Android应用程序,我想根据用户的性别和年龄自动设置。Android:如何获取用户的性别和年龄?

有哪些不同的方式来获得的年龄和用户的性别? (这是谷歌Play政策标准)

例如,有没有办法让通过谷歌Play服务,这些信息?

谢谢。

+0

您可以通过[Google+ API](https://developers.google.com/+/api/latest/people)获取他们 –

+1

问题是PlusClient已被弃用http://www.doc.ic.ac .uk/project/2013/271/g1327125/android-studio/sdk/extras/google/google_play_services/docs/reference/com/google/android/gms/plus/PlusClient.html并由GoogleApiClient取代http:// www。 doc.ic.ac.uk/project/2013/271/g1327125/android-studio/sdk/extras/google/google_play_services/docs/reference/com/google/android/gms/common/api/GoogleApiClient.html但我不没有看到如何通过它来获得年龄和性别。 –

回答

10

您应该使用interface Person,你有你需要了解用户的一切。 (通过getGender()getBirthday()(或者getAgeRange()

编辑: 对于使用例如假设getGender(),你会怎么做解决这个事情:

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(); 
+0

谢谢!你有一些示例代码? Regards –

+0

@Regis_AG我编辑了我对你提出的示例代码的回答,它可能缺少一些小东西,因为我现在不在我的主计算机上,但它会做的。玩的开心 ! – Mekap

+0

谢谢!我只是尝试了一个接近你写的代码(主要区别在于getCurrentPerson必须在onConnected回调中)。 我很伤心的结果。我尝试了3个不同的Gmail帐户(我的,我母亲和妹妹的),大部分的Person.getSomething函数都返回null或者一些不完整/不准确的东西。 getBirthdayDate:返回在3例无效 getAgeRange:返回null 2 3次以上(一次只是一个分年龄(21)远离我的真实年龄(36)) getGender:可靠 getCurrentLocation:空 使用getLanguage:空1次,确定2次 结论:只有性别可靠。 –

3

另一种,也许更可靠,方法来猜测你的用户在Android中的性别会用你的用户的电子邮件地址打到第三方API,然后让API在电子邮件字符串中查找名字,将其与名称数据库进行匹配,然后返回给你一个性别和置信度级别,我用“性别API”(无从属关系)

只需要添加到AndroidManifest:

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

然后让你的用户的Gmail地址:

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 


} 
} 

然后,如果你想用我所用的API,摆脱性别的API的关键。 COM和打这个链接,每个电子邮件地址:

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

此API返回的性别和自信的水平,我敢肯定有其他人那里做同样的事情。

+1

这很好,谢谢分享。有没有类似的API可以提供一个年龄? – AlexVPerl

相关问题