码迷,mamicode.com
首页 > 移动开发 > 详细

分页读取手机联系人

时间:2014-12-21 15:19:05      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:分页读取手机联系人


<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" />

分页读取手机联系人

标签:分页读取手机联系人

原文地址:http://blog.csdn.net/xiaoyi_tdcq/article/details/42060497

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!