select Contacts Info - Android Account

Android examples for Account:Contact

Description

select Contacts Info

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.ContentResolver;

import android.database.Cursor;
import android.net.Uri;

public class Main {
    private static String uri_rawcontacts = "content://com.android.contacts/raw_contacts";
    private static String uri_contacts_phones = "content://com.android.contacts/data/phones";
    private static String uri_contacts_emails = "content://com.android.contacts/data/emails";

    public static List<Map<String, Object>> selectContactsInfo(
            ContentResolver resolver) {/*from  w ww . ja  v a 2  s .  c  om*/
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Cursor contactsCursor = resolver.query(Uri.parse(uri_rawcontacts),
                new String[] { "_id", "display_name" }, null, null, null);
        while (contactsCursor.moveToNext()) {
            Map<String, Object> map = new HashMap<String, Object>();
            int contactsId = contactsCursor.getInt(contactsCursor
                    .getColumnIndex("_id"));
            String displayName = contactsCursor.getString(contactsCursor
                    .getColumnIndex("display_name"));
            map.put("_id", contactsId);
            map.put("display_name", displayName);

            Cursor phoneCursor = resolver.query(
                    Uri.parse(uri_contacts_phones), new String[] {
                            "raw_contact_id", "data1" },
                    "raw_contact_id=?", new String[] { contactsId + "" },
                    null);

            StringBuilder sb = new StringBuilder();
            while (phoneCursor.moveToNext()) {
                sb.append(phoneCursor.getString(1));
                sb.append("|");
            }
            map.put("phones", sb.toString());
            if (phoneCursor != null) {
                phoneCursor.close();
            }

            Cursor emailCursor = resolver.query(
                    Uri.parse(uri_contacts_emails), new String[] {
                            "raw_contact_id", "data1" },
                    "raw_contact_id=?", new String[] { contactsId + "" },
                    null);

            StringBuilder sb2 = new StringBuilder();
            while (emailCursor.moveToNext()) {
                sb2.append(emailCursor.getString(1));
                sb2.append("|");
            }
            map.put("emails", sb2.toString());
            if (emailCursor != null) {
                emailCursor.close();
            }

            list.add(map);
        }
        if (contactsCursor != null) {
            contactsCursor.close();
        }
        return list;
    }
}

Related Tutorials