【问题标题】:how to get firstname and lastname of Android phone owner?如何获取 Android 手机所有者的名字和姓氏?
【发布时间】:2012-10-16 14:17:51
【问题描述】:

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

【问题讨论】:

    标签: android contacts


    【解决方案1】:

    我认为这仅适用于 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 权限。

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

    【讨论】:

    • 至少,如果它是 ICS,我该如何执行此代码? ContactsContract.Profile.CONTENT_URI 给了我错误。
    • 您必须在清单中将 android:targetSdkVersion="16" 添加到您的 uses-sdk,因为它是 ICS 及更高版本
    • 它对你有用吗?或者您需要更多帮助来设置它吗?
    【解决方案2】:

    试试这个:

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

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 2011-11-14
      • 1970-01-01
      • 2019-01-27
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多