2015-04-16 30 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

相關問題