Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取Android手机所有者的名字和姓氏?_Android_Contacts - Fatal编程技术网

如何获取Android手机所有者的名字和姓氏?

如何获取Android手机所有者的名字和姓氏?,android,contacts,Android,Contacts,有没有办法获得Android手机所有者的名字和姓氏?我在网上搜索过,但运气不好。我无意中发现了下面的问题,但这是所有联系人的名字和姓氏。我需要的只是得到主人的名字和姓氏 试试这个: final AccountManager manager = AccountManager.get(this); final Account[] accounts = manager.getAccountsByType("com.google"); final int size = accounts.length;

有没有办法获得Android手机所有者的名字和姓氏?我在网上搜索过,但运气不好。我无意中发现了下面的问题,但这是所有联系人的名字和姓氏。我需要的只是得到主人的名字和姓氏

试试这个:

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;
}

我想这只适用于ICS以后的版本-更多信息请查看此版本

Android包含一个代表设备所有者的个人配置文件-此配置文件称为Me配置文件,存储在ContactsContract.profile表中。只要您愿意,您就可以从用户配置文件中读取数据

在AndroidManifest.xml中添加READ_配置文件和READ_联系人权限


与您最相关的字段是联系人中的“显示姓名”列,也可能是StructuredName字段

我搜索了这件事,得到了一些喜欢的内容,希望对您有所帮助


至少,如果是ICS,我如何执行这段代码?ContactsContract.Profile.CONTENT\u URI给我错误。您必须将android:targetSdkVersion=16添加到清单中的uses sdk,因为它是ICS及以上版本。它对您有效吗?或者你需要更多的帮助来设置它吗?
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();