获取联系人在Android开发中经常会遇到,最近项目需要用到获取手机联系人的功能,于是写了一个先查询联系人id然后再根据id查询手机号demo,结果慢到怀疑人生(亲测联系人数据1500+)。最后经过优化查询方式后,查询时间在200ms左右(亲测联系人数据1500+)在这里分享给大家,以方便自己好直接拿来用。国际惯例,注:以下测试只查询名字和手机号码
1、查询所有联系人
/**
* 获取所有联系人
*
* @param context 上下文
* @return 联系人集合
*/
public List<PhoneBookEntity> getContacts(Context context) {
long currentTimeMillis = System.currentTimeMillis();
contacts.clear();
Cursor cursor = null;
ContentResolver cr = context.getContentResolver();
try {
cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, "sort_key");
if (cursor != null) {
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String mobileNo, displayName;
while (cursor.moveToNext()) {
mobileNo = cursor.getString(mobileNoIndex);
displayName = cursor.getString(displayNameIndex);
contacts.add(new PhoneBookEntity(displayName, mobileNo));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
Log.d(TAG, "获取所有联系人耗时: " + (System.currentTimeMillis() - currentTimeMillis) + ",共计:" + cursor.getCount());
cursor.close();
}
}
return contacts;
}
2、通过姓名获取联系人
/**
* 通过姓名获取联系人
*
* @param context 上下文
* @param contactName 联系人姓名
* @return 联系人集合
*/
public List<PhoneBookEntity> getContactByName(Context context, String contactName) {
long currentTimeMillis = System.currentTimeMillis();
contacts.clear();
Cursor cursor = null;
ContentResolver cr = context.getContentResolver();
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " like ? ";
String[] selectionArgs = new String[]{"%" + contactName + "%"};
try {
cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, selection, selectionArgs, "sort_key");
if (cursor != null) {
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String mobileNo, displayName;
while (cursor.moveToNext()) {
mobileNo = cursor.getString(mobileNoIndex);
displayName = cursor.getString(displayNameIndex);
contacts.add(new PhoneBookEntity(displayName, mobileNo));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
Log.d(TAG, "通过姓名获取联系人耗时: " + (System.currentTimeMillis() - currentTimeMillis));
}
return contacts;
}
3、通过手机号码查询联系人
/**
* 通过手机号获取联系人
*
* @param context 上下文
* @param number 手机号码
* @return 联系人集合
*/
public List<PhoneBookEntity> getContactByNumber(Context context, String number) {
long currentTimeMillis = System.currentTimeMillis();
contacts.clear();
Cursor cursor = null;
ContentResolver cr = context.getContentResolver();
String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " like ? ";
String[] selectionArgs = new String[]{"%" + number + "%"};
try {
cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, selection, selectionArgs, "sort_key");
if (cursor != null) {
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String mobileNo, displayName;
while (cursor.moveToNext()) {
mobileNo = cursor.getString(mobileNoIndex);
displayName = cursor.getString(displayNameIndex);
contacts.add(new PhoneBookEntity(displayName, mobileNo));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
Log.d(TAG, "通过手机号获取联系人耗时: " + (System.currentTimeMillis() - currentTimeMillis));
}
return contacts;
}
最后附上源码:
package com.welasda.wela;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
* ProjectName:welasda
* Author:Gnoxu
* Date:2018/6/13
* Time:10:18
* Description:获取联系人工具类
*/
public class ContactHelper {
private static final String TAG = "ContactHelper";
private static final String[] PROJECTION = new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
private List<PhoneBookEntity> contacts = new ArrayList<>();
private ContactHelper() {
}
public static ContactHelper getInstance() {
return InstanceHolder.INSTANCE;
}
private static class InstanceHolder {
private static final ContactHelper INSTANCE = new ContactHelper();
}
/**
* 获取所有联系人
*
* @param context 上下文
* @return 联系人集合
*/
public List<PhoneBookEntity> getContacts(Context context) {
long currentTimeMillis = System.currentTimeMillis();
contacts.clear();
Cursor cursor = null;
ContentResolver cr = context.getContentResolver();
try {
cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, "sort_key");
if (cursor != null) {
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String mobileNo, displayName;
while (cursor.moveToNext()) {
mobileNo = cursor.getString(mobileNoIndex);
displayName = cursor.getString(displayNameIndex);
contacts.add(new PhoneBookEntity(displayName, mobileNo));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
Log.d(TAG, "获取所有联系人耗时: " + (System.currentTimeMillis() - currentTimeMillis) + ",共计:" + cursor.getCount());
cursor.close();
}
}
return contacts;
}
/**
* 通过姓名获取联系人
*
* @param context 上下文
* @param contactName 联系人姓名
* @return 联系人集合
*/
public List<PhoneBookEntity> getContactByName(Context context, String contactName) {
long currentTimeMillis = System.currentTimeMillis();
contacts.clear();
Cursor cursor = null;
ContentResolver cr = context.getContentResolver();
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " like ? ";
String[] selectionArgs = new String[]{"%" + contactName + "%"};
try {
cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, selection, selectionArgs, "sort_key");
if (cursor != null) {
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String mobileNo, displayName;
while (cursor.moveToNext()) {
mobileNo = cursor.getString(mobileNoIndex);
displayName = cursor.getString(displayNameIndex);
contacts.add(new PhoneBookEntity(displayName, mobileNo));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
Log.d(TAG, "通过姓名获取联系人耗时: " + (System.currentTimeMillis() - currentTimeMillis));
}
return contacts;
}
/**
* 通过手机号获取联系人
*
* @param context 上下文
* @param number 手机号码
* @return 联系人集合
*/
public List<PhoneBookEntity> getContactByNumber(Context context, String number) {
long currentTimeMillis = System.currentTimeMillis();
contacts.clear();
Cursor cursor = null;
ContentResolver cr = context.getContentResolver();
String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " like ? ";
String[] selectionArgs = new String[]{"%" + number + "%"};
try {
cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, selection, selectionArgs, "sort_key");
if (cursor != null) {
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String mobileNo, displayName;
while (cursor.moveToNext()) {
mobileNo = cursor.getString(mobileNoIndex);
displayName = cursor.getString(displayNameIndex);
contacts.add(new PhoneBookEntity(displayName, mobileNo));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
Log.d(TAG, "通过手机号获取联系人耗时: " + (System.currentTimeMillis() - currentTimeMillis));
}
return contacts;
}
/**
* 分页查询联系人
*
* @param context 上下文
* @param page 页数
* @param pageSize 每页数据量
* @return 联系人集合
*/
public List<PhoneBookEntity> getContactsByPage(Context context, int page, int pageSize) {
long currentTimeMillis = System.currentTimeMillis();
contacts.clear();
Cursor cursor = null;
ContentResolver cr = context.getContentResolver();
try {
String sortOrder = "_id limit " + page + "," + pageSize;
cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, sortOrder);
if (cursor != null) {
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int mobileNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String mobileNo, displayName;
while (cursor.moveToNext()) {
mobileNo = cursor.getString(mobileNoIndex);
displayName = cursor.getString(displayNameIndex);
contacts.add(new PhoneBookEntity(displayName, mobileNo));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
Log.d(TAG, "分页查询联系人耗时: " + (System.currentTimeMillis() - currentTimeMillis));
}
return contacts;
}
/**
* 获取联系人总数
*
* @param context 上下文
* @return 数量
*/
public int getContactCount(Context context) {
long currentTimeMillis = System.currentTimeMillis();
Cursor cursor = null;
ContentResolver cr = context.getContentResolver();
try {
cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, "sort_key");
if (cursor != null) {
return cursor.getCount();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
Log.d(TAG, "获取联系人总数耗时: " + (System.currentTimeMillis() - currentTimeMillis));
}
return 0;
}
}