根据群组查询联系人-android系统联系人

//根据分组查询联系人的projection zjm add

public static final String[] RAW_PROJECTION = new String[] { Data.RAW_CONTACT_ID, };
//根据分组查询联系人的selection zjm add
public static final String RAW_CONTACTS_WHERE = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
+ "=?"
+ " and "
+ Data.MIMETYPE
+ "="
+ "'"
+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
+ "'";
//查询未分组联系人的过滤条件
public static final String RAW_CONTACTS_IN_NO_GROUP_SELECTION = "1=1) and " 
+ Data.RAW_CONTACT_ID 
+ " not in( select " 
+ Data.RAW_CONTACT_ID 
+ " from view_data_restricted where " 
+ Data.MIMETYPE 
+ "='" 
+ GroupMembership.CONTENT_ITEM_TYPE 
+ "') group by (" 
+ Data.RAW_CONTACT_ID;

//查询可显示且未删除的群组的过滤条件
public static final String VISIBLE_GROUP_SELECTION = ContactsContract.Groups.GROUP_VISIBLE 
+ "=1 and "
+ ContactsContract.Groups.DELETED 

+ "=0";

/***
* 获取所有未分组的联系人的rawId和name zjm add
*/
private void getContactsNullCircles(){
List<ContactsBean> conBeanList = new ArrayList<ContactsBean>();
//查询未分组联系人的rawId 和 name
Cursor cursor = mContentResolver.query(
                Data.CONTENT_URI,
                null,
                RAW_CONTACTS_IN_NO_GROUP_SELECTION, 
                null, null);

int rawIdIndex = cursor.getColumnIndex(Data.RAW_CONTACT_ID);

int i=0;
while(cursor.moveToNext()){
long rawId = cursor.getLong(rawIdIndex);
String conName = getConDisplayNameByRawId(rawId);

Log.i("raw id & name & num =>", rawId + "&&" + conName + "&&" + i++);
}

}
/***
* 获取各分组的联系人的rawId和name,及分组的id zjm add
*/
private void getContactsInCircles(){
Cursor circleCursor = mContentResolver.query(Groups.CONTENT_URI, null,
VISIBLE_GROUP_SELECTION, null,
null);

int circleIdIndex = circleCursor.getColumnIndex(Groups._ID);

//这里查询到的群组名字不是真正的显示名字,其实对应的应该是Group表中的display_title字段,

//但是貌似没有呢~表里有这个字段,但是api不提供这个字段的查询
int circleNameIndex = circleCursor.getColumnIndex(Groups.TITLE);

while (circleCursor.moveToNext()) {
long circleId = circleCursor.getLong(circleIdIndex);
String circleName = circleCursor.getString(circleNameIndex);

Log.i("group id & name =>", circleId + "&&" + circleName);

Cursor conRawIdCursor = mContentResolver.query(Data.CONTENT_URI, RAW_PROJECTION,  
         RAW_CONTACTS_WHERE,   
         new String[]{"" + circleId},  
         null);

//这里查询的是系统联系人data表中的RAW_CONTACT_ID字段,获取联系人的rawId
int rawIdIndex = conRawIdCursor.getColumnIndex(Data.RAW_CONTACT_ID);
int i =0;

while(conRawIdCursor.moveToNext()){
long conRawId = conRawIdCursor.getLong(rawIdIndex);
String conName = getConDisplayNameByRawId(conRawId);

Log.i("####group id & name =>", circleId + "&&" + circleName);
Log.i("raw id & name & num =>", conRawId + "&&" + conName + "&&" + i++);
}

}
}

/***
* 根据联系人rawId获取displayName  zjm add
* @param rawId
* @return
*/
private String getConDisplayNameByRawId(long rawId){
String[] name = getNameByRawId(rawId);

String displayName = null;
if (name[2] != null) {
displayName = name[2];
} else if (name[0] != null) {
displayName = name[0] + "" + name[1];
} else if (name[1] != null) {
displayName = name[1];
}

return displayName;
}


/**
* 根据Id 得到 familyName 和 giveName [1,0]
*/
public String[] getNameByRawId(long rawId) {
String name[] = new String[3];
Cursor cursor = null;
try {
cursor = getContentResolver().query(Data.CONTENT_URI, null,
Data.RAW_CONTACT_ID + " = " + rawId + " and "
+ Data.MIMETYPE + " = '"
+ StructuredName.CONTENT_ITEM_TYPE + "'", null,
null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
name[0] = cursor.getString(cursor
.getColumnIndex(StructuredName.FAMILY_NAME));
if (name[0] == null) {
name[0] = "";
}
name[1] = cursor.getString(cursor
.getColumnIndex(StructuredName.GIVEN_NAME));
if (name[1] == null) {
name[1] = "";
}
name[2] = cursor.getString(cursor
.getColumnIndex(StructuredName.DISPLAY_NAME));
if (name[2] == null) {
name[2] = name[0] + name[1];
}


return name;
}
return null;
} catch (Exception e) {


return null;
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}


}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值