欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

android分页查询获取系统联系人信息

来源: 开发者 投稿于  被查看 20980 次 评论:202

android分页查询获取系统联系人信息


package com.example.yqqmobilesafe.ContactProvider;

import java.util.ArrayList;
import java.util.List;


import android.R.integer;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import android.provider.ContactsContract;



import com.example.yqqmobilesafe.domain.ContactInfo;

public class ContactInfoProvider {
	private Context mContext;
	public ContactInfoProvider(Context context) {
		mContext=context;
		
	}
	/**
	 * 获取系统联系人信息
	 * @return
	 */
	public  List getSystemContactInfos(){
		List infos=new ArrayList();
		
		// 使用ContentResolver查找联系人数据
		Cursor cursor = mContext.getContentResolver().query(
			ContactsContract.Contacts.CONTENT_URI, null, null,
			null, null);
		
		// 遍历查询结果,获取系统中所有联系人
		while (cursor.moveToNext())
		{
			ContactInfo info=new ContactInfo();
			// 获取联系人ID
			String contactId = cursor.getString(cursor
				.getColumnIndex(ContactsContract.Contacts._ID));
			// 获取联系人的名字
			String name = cursor.getString(cursor.getColumnIndex(
				ContactsContract.Contacts.DISPLAY_NAME));
			info.setContactName(name);
			
			// 使用ContentResolver查找联系人的电话号码
			Cursor phones = mContext.getContentResolver().query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
				null,
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID
					+ " = " + contactId, null, null);
			
			// 遍历查询结果,获取该联系人的多个电话号码
			while (phones.moveToNext())
			{
				// 获取查询结果中电话号码列中数据。
				String phoneNumber = phones.getString(phones
					.getColumnIndex(ContactsContract
					.CommonDataKinds.Phone.NUMBER));
				info.setPhoneNumber(phoneNumber);
			}
			phones.close();
			
			infos.add(info);
			info=null;
		}
		cursor.close();
		
		return infos;
		
	}
	
	/**
	 * 分页查询系统联系人信息
	 * @param pageSize 每页最大的数目
	 * @param currentOffset 当前的偏移量
	 * @return
	 */
	public List getContactsByPage(int pageSize,int currentOffset) {
		
		List infos=new ArrayList();
				Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
				String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
										ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key"};
				Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, "sort_key COLLATE LOCALIZED asc limit " + pageSize + " offset " + currentOffset);
				if (cursor != null) {
					
					while (cursor.moveToNext()) {
						
						ContactInfo info=new ContactInfo();
						String contactName = cursor.getString(0);
						String phoneNumber = cursor.getString(1);
						info.setContactName(contactName);
						info.setPhoneNumber(phoneNumber);
						infos.add(info);
						info=null;
					}
					cursor.close();
				
			
	}
				return infos;
	}
	
	/**
	 * 获得系统联系人的所有记录数目
	 * @return
	 */
	public int getAllCounts(){
		int num=0;
		// 使用ContentResolver查找联系人数据
				Cursor cursor = mContext.getContentResolver().query(
					ContactsContract.Contacts.CONTENT_URI, null, null,
					null, null);
				
				// 遍历查询结果,获取系统中所有联系人
				while (cursor.moveToNext())
				{
					num++;
				}
				cursor.close();
	

		return num;
	}
	

}

用户评论