Android查询所有联系人和根据号码查询联系人方法

直接看例子:

package com.msg.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;

public class ToolUtil {
private Context context;

public ToolUtil(Context context) {
this.context = context;
}

public List<Map<String, String>> getContactList() {
List<Map<String, String>> items = new ArrayList<Map<String, String>>();

Cursor cur = null;
try {
// Query using ContentResolver.query or Activity.managedQuery
cur = this.context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
if (cur.moveToFirst()) {
int idColumn = cur
.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
// Iterate all users
do {
String contactId;
String displayName;
String phoneNumber = "";
// Get the field values
contactId = cur.getString(idColumn);
displayName = cur.getString(displayNameColumn);
// Get number of user's phoneNumbers
int numberCount = cur
.getInt(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (numberCount > 0) {
Cursor phones = this.context
.getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId
/*
* + " and " +
* ContactsContract.CommonDataKinds
* .Phone.TYPE + "=" +
* ContactsContract.CommonDataKinds
* .Phone.TYPE_MOBILE
*/, null, null);
if (phones.moveToFirst()) {
int numberColumn = phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// Iterate all numbers
do {
phoneNumber += phones.getString(numberColumn)
+ ",";
} while (phones.moveToNext());
}
}
// Add values to items
Map<String, String> i = new HashMap<String, String>();
i.put("name", displayName);
i.put("key", phoneNumber);
items.add(i);
} while (cur.moveToNext());
} else {
Map<String, String> i = new HashMap<String, String>();
i.put("name", "Your Phone");
i.put("key", "Have No Contacts.");
items.add(i);
}
} finally {
if (cur != null)
cur.close();
}
return items;
}

public String getPeople(String number) {
String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
// 将自己添加到 msPeers 中
Cursor cursor = this.context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, // Which columns to return.
ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
+ number + "'", // WHERE clause.
null, // WHERE clause value substitution
null); // Sort order.
if (cursor != null) {
for (int i = 0; i < cursor.getCount(); i++){
cursor.moveToPosition(i);
// 取得联系人名字
int nameFieldColumnIndex = cursor
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String name = cursor.getString(nameFieldColumnIndex);// 这里提示
// force
// close
return name;
}
}
return "";


}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值