<span style="font-size:18px;">/** * 手机联系人个数 */ public static int linkManCount = -1; /** * 每页显示联系人个数 */ public static int pageCount = 20;</span>
<span style="font-size:18px;">/** * 获取手机联系人,并返回 * * @param mContext * 上下文对象 * @param pageSize * 当前需要获取第几页数据 * @return */ public ArrayList<helperPoSort> getLinkMan(Context mContext, int pageSize ) { ArrayList<helperPoSort> linkMans = new ArrayList<helperPoSort>(); ContentResolver cr = mContext.getContentResolver(); // 取得电话本中开始一项的光标 Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); // 向下移动光标 linkManCount = cursor.getCount(); if (cursor != null && ((pageSize - 1) * pageCount) < linkManCount) { cursor.moveToPosition((pageSize - 1) * pageCount); int i = 0; while (cursor.moveToNext()) { if (i < pageCount) { Cursor phone = null; String contact = null; try { // 取得联系人名字 int nameFieldColumnIndex = cursor .getColumnIndex(PhoneLookup.DISPLAY_NAME); contact = cursor.getString(nameFieldColumnIndex); // 取得电话号码 String ContactId = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID)); phone = cr .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null); } catch (Exception e) { LogUtil.d("TAG", "获取联系人时,在游标获取phone时报错!"); break; } LogUtil.d("TAG", "Cursor phone" + phone); if (phone != null) { while (phone.moveToNext()) { String Number = phone .getString(phone .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // string += (contact + ":" + Number + ""); helperPoSort man = new helperPoSort(); man.setStr1(contact); man.setStr2(Number); LogUtil.d("TAG", contact + ":" + Number + ""); linkMans.add(man); } } i = i + 1; } else { break; } } } cursor.close(); return linkMans; }</span>
需要权限:<uses-permission android:name="android.permission.READ_CONTACTS" />
时间: 2024-11-03 22:10:14