清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
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<ContactInfo> getSystemContactInfos(){ List<ContactInfo> infos=new ArrayList<ContactInfo>(); // 使用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<ContactInfo> getContactsByPage(int pageSize,int currentOffset) { List<ContactInfo> infos=new ArrayList<ContactInfo>(); 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; } }