9

有没有办法获取 Android 手机所有者的名字和姓氏?我已经搜索了互联网,但我没有运气。我在 Stackoverlow 中偶然发现了这个问题,但这是获取所有联系人的名字和姓氏。我需要的只是获取所有者的名字和姓氏。

4

3 回答 3

23

我认为这仅适用于 ICS - 查看更多信息http://android-codelabs.appspot.com/resources/tutorials/contactsprovider/ex1.html

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)));
        ...
        //Use the values
    }
}
c.close();

Android 包含一个代表设备所有者的个人配置文件 - 此配置文件称为“我”配置文件并存储在ContactsContract.Profile表中。您可以从用户的个人资料中读取数据,只要您

在您的 AndroidManifest.xml 中添加 READ_PROFILE 和 READ_CONTACTS 权限。

与您最相关的字段是 Contact 中的 DISPLAY_NAME 列,可能还有 StructuredName 字段

于 2012-10-30T10:16:24.880 回答
3

我搜索了这个东西并获得了一些喜欢,希望它对你有帮助。

于 2012-11-05T08:19:40.000 回答
3

尝试这个:

final AccountManager manager = AccountManager.get(this);
final Account[] accounts = manager.getAccountsByType("com.google");
final int size = accounts.length;
String[] names = new String[size];
for (int i = 0; i < size; i++) {
  names[i] = accounts[i].name;
}
于 2012-10-26T10:07:04.223 回答