gpt4 book ai didi

java - 如何在 Android 上获取联系人的名字和姓氏列表?

转载 作者:行者123 更新时间:2023-12-01 15:05:26 27 4
gpt4 key购买 nike

Possible Duplicate:
How to read contacts on Android 2.0

我只想要一个List<String> Android 设备联系人列表中的名字 + 姓氏。应该是非常简单的事情,但我找不到任何基本代码。

只是寻找:

Steve Smith
Joe Shmo
Blah Davis

等等。全部来自用户手机上的联系人。我已经添加了:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 

最佳答案

    public List<String> getFullContactName(){

List<String> name = new List<String>();

String[] projection = new String[] {Data.DATA2, Data.DATA3};
String where = Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'";
Uri uri = Data.CONTENT_URI;

ContentResolver contentResolver = context.getContentResolver();
// Make the query.
Cursor cursor = contentResolver.query(uri,
projection, // Which columns to return
where, // Which rows to return
null, // Selection arguments (none)
// Put the results in ascending order by name
null);

String firstName, LastName;
while (cursor.moveToNext()) {

firstName = cursor.getString(cursor.getColumnIndex(Data.DATA2));
lastName = cursor.getString(cursor.getColumnIndex(Data.DATA3));
name.add(firstName + " " + lastName);
}

cursor.close();
cursor = null;
return name;
}

关于java - 如何在 Android 上获取联系人的名字和姓氏列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13019258/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com