如何检索Android的联系人列表 [英] how to retrieve the list of contacts in android
本文介绍了如何检索Android的联系人列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的目的检索联系人(姓名,号码)被记录在一个JSON对象,通过web服务来发送到服务器的列表
my purpose to retrieve the list of contacts (name, number) is recorded in a json object, to send via web service to the server
后来我发现了code访问联系人,这是件好事,我测试这个code
then I found the code to visit contacts, and it is good, I test this code
String id, name;
ContentResolver crs = getContentResolver();
Cursor people = crs.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = "";
people.moveToPosition(Contexts.getnbContacts());
while (people.moveToNext())
{
id = people.getString(people
.getColumnIndex(ContactsContract.Data._ID));
name = people.getString(people
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
if (Integer
.parseInt(people.getString(people
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
while (phones.moveToNext()) {
// pdata =
// phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
phone = phone
+ phones.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA))
+ " and ";
;
}
phones.close();
}
我的问题是:如何保存每个联系人(姓名,号码)的数组或数组列表...
My question is: how to save each contact (name, number) in an array or arraylist ...
每个表行指定的联系人
我觉得数组类型是使用JSON格式
I think the array type is compatible with json format
所以如何将联系人复制的JSON对象
so how to copy the contacts in a json object
推荐答案
我在这里的code
public static Map<String, String> getAddressBook(Context context)
{
Map<String, String> result = new HashMap<String, String>();
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while(cursor.moveToNext())
{
int phone_idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int name_idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String phone = cursor.getString(phone_idx);
String name = cursor.getString(name_idx);
result.put(name, phone);
}
cursor.close();
return result;
}
和必要性
<uses-permission android:name="android.permission.READ_CONTACTS"/>
这篇关于如何检索Android的联系人列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文