Android - 使用手机所有者的 AccountManager/First and Last name 获取 UserData

标签 android android-contacts

我想在我的应用程序中预填充一些字段,以便在用户订阅我的应用程序内的服务时帮助他。

那么我如何获得设备所有者的名字和姓氏。我想使用与 Google 帐户关联的默认信息;到目前为止我得到了这个:

AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();
for (Account account : accounts) {
    if (account.type.compareTo("com.google") == 0)
    {
        String possibleEmail = account.name;
        // how to get firstname and lastname here?
    }             
}

如果您提出建议,我愿意采取其他方法 - 只要我能得到所有者的电子邮件、名字和姓氏。

最佳答案

Ice Cream Sandwich获取此信息很容易,因为 Android 包含代表设备所有者的个人配置文件 - 此配置文件称为“我”配置文件并存储在 ContactsContract.Profile 中。 table 。只要您在 AndroidManifest.xml 中请求 READ_PROFILEREAD_CONTACTS 权限,就可以从用户的个人资料中读取数据。

与您最相关的字段是 DISPLAY_NAME来自联系人的列,可能还有 StructuredName字段 - 用户的联系照片之类的东西也可用。

Android Code Lab 教程给出了 a full example of reading a user's profile ,代码的核心位在 ListProfileTask 中。这是一个删节的 fragment :

Cursor c = activity.getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
int count = c.getCount();
String[] columnNames = c.getColumnNames();
boolean b = c.moveToFirst();
int position = c.getPosition();
if (count == 1 && position == 0) {
    for (int j = 0; j < columnNames.length; j++) {
        String columnName = columnNames[j];
        String columnValue = c.getString(c.getColumnIndex(columnName)));
        ...
        // consume the values here
    }
}
c.close();

不幸的是,我认为在 API 级别 14 之前没有办法获取此类数据。

关于Android - 使用手机所有者的 AccountManager/First and Last name 获取 UserData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7367283/

相关文章:

android - 如何从给定名称中获取建议的联系人列表

android - 删除特定的 RawContact 内容目录条目

android - 在 Android 上,使用预填充的联系表打开添加新联系人的正确方法是什么?

java - 我如何知道 View 是否已绘制

android - 背景图片右下角对齐错误 - Android 上的 Chrome

android - 仅显示一定数量的联系人

android - 在现有联系人个人资料 android 中添加应用程序链接

android - 导航 View 。 android.support.design :itemIconTint , android.support.design:itemTextColor xml 属性不起作用

java - 从 arraylist 和 hashmap 中删除重复项

android - 实现Android布局TableView