如何在Android设备上获取用户个人资料

Dan*_*son 2 android google-account contactscontract accountmanager android-contacts

我希望能够自动将信息输入到创建帐户表单中,并根据用户已输入设备的信息(如姓名,电子邮件地址,电话号码)建议可能性.我需要一种与API级别8兼容的方法.

Dan*_*son 5

我找到了Roman Nurik对一个非常相似的答案的答案,因此我的答案是基于他的答案.这是我的答案要点.您需要添加权限和功能AndroidManifest.xml才能访问用户的个人资料:

<!-- Allows application to view phone state like using readLine1Number() -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!-- Required to access the Contacts Provider and user profile -->
<uses-permission android:name="android.permission.READ_PROFILE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

<!-- Allows the application to use telephony to get the devices phone number when telephony is available without requiring telephony -->
<uses-feature android:name="android.hardware.telephony" android:required="false"/>
Run Code Online (Sandbox Code Playgroud)

该方法使用Roman描述的两种方法之一,基于设备支持的API级别.它显示了如何利用用户设置的主要字段以及何时设置多个值.它使用它TelephonyManager来检索设备的电话号码.