android读取手机联系人

    xiaoxiao2024-11-28  10

    private ArrayList<HashMap<String, String>> readContacts() { ArrayList<HashMap<String, String>> contacts = new ArrayList<HashMap<String, String>>(); ContentResolver resolver = getContentResolver(); Uri uriRaw = Uri.parse("content://com.android.contacts/raw_contacts");// raw_contacts表的uri Uri uriData = Uri.parse("content://com.android.contacts/data");// data表的uri Cursor cursor = resolver.query(uriRaw, new String[] { "contact_id" }, null, null, null); if (cursor != null) { while (cursor.moveToNext()) { String id = cursor.getString(0); Cursor dataCursor = resolver.query(uriData, new String[] { "data1", "mimetype" }, "raw_contact_id=?", new String[] { id }, null); if (dataCursor != null) { HashMap<String, String> map = new HashMap<String, String>(); while (dataCursor.moveToNext()) { String data = dataCursor.getString(0); String mimeType = dataCursor.getString(1); if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { map.put("phone", data);// 设置手机号码 } else if ("vnd.android.cursor.item/name" .equals(mimeType)) { map.put("name", data);// 设置名称 } } contacts.add(map); } } } return contacts; } SimpleAdapter adapter = new SimpleAdapter(this, contacts, R.layout.list_contact_item, new String[] { "name", "phone" }, new int[] { R.id.tv_name, R.id.tv_phone }); lvList.setAdapter(adapter);

    需要配置权限 uses-permission android:name=”android.permission.READ_CONTACTS”

    转载请注明原文地址: https://ju.6miu.com/read-1294038.html
    最新回复(0)