android中搜索联系人

    public static final int COLUMN_INDEX_DISPLAYNAME = 1;
    public static final int COLUMN_INDEX_SEARCHENUMBER = 2;
    public static final String SEARCH_URL = "content://com.android.contacts/search_suggest_query/";    
    public static ArrayList searchContactsWithSuggest(Context context, String suggest) {
        Cursor cursor = null;
        ArrayList contacts = null;

        try {
            Uri uri = Uri.parse(SEARCH_URL + suggest + "?limit=100");
            cursor = context.getContentResolver().query(uri, null, null, null, null);
            if (cursor != null) {
                contacts = new ArrayList();

                cursor.moveToPosition(-1);
                while (cursor.moveToNext()) {
                    String displayName = cursor.getString(COLUMN_INDEX_DISPLAYNAME);
                    String number = cursor.getString(COLUMN_INDEX_SEARCHENUMBER);

                    ContactInfo info = new ContactInfo(ContactInfo.TYPE_CONTACT, 0, 0, displayName, null, null, false);
                    DataBean dataBean = new DataBean(ContactInfo.TYPE_CONTACT, number);
                    DataBean[] list = new DataBean[] { dataBean };
                    info.setDataBeans(list);
                    contacts.add(info);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        return contacts;
    } 
  

你可能感兴趣的:(android学习笔记)