my purpose to retrieve the list of contacts (name, number) is recorded in a json object, to send via web service to the server
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 ...
each table row designated contact
I think the array type is compatible with json format
so how to copy the contacts in a json object
here my 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;
}
and need
<uses-permission android:name="android.permission.READ_CONTACTS"/>
您可以使用以下查询从Contacts DB中检索收藏的联系人
Cursor cursor = this.managedQuery(ContactsContract.Contacts.CONTENT_URI, projection, "starred=?",new String[] {"1"}, null);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.