网上获取的:
* 根据电话号码取得联系人姓名
*/
/*
public static String getContactNameByPhoneNumber(Context context,
String address) {
String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
// 将自己添加到 msPeers 中
Cursor cursor = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, // Which columns to return.
ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
+ address + "'", // WHERE clause.
null, // WHERE clause value substitution
null); // Sort order.
if (cursor == null) {
Log.d(TAG, "getPeople null");
return 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);
return name;
}
return null;
}
/**
* 获取所有联系人内容
*
* @param context
* @param address
* @return
*/
/*
public static String getContacts(Context context) {
StringBuilder sb = new StringBuilder();
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cursor.moveToFirst()) {
do {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// 第一条不用换行
if (sb.length() == 0) {
sb.append(name);
} else {
sb.append("\n" + name);
}
Cursor phones = cr.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));
// 添加Phone的信息
sb.append("\t").append(phoneNumber);
}
phones.close();
} while (cursor.moveToNext());
}
cursor.close();
return sb.toString();
}
这段代码无法查找sim卡联系人的信息!而且cursor没有关闭,导致许多错误。
自己改良后的代码如下:
private static final Uri PHONES_WITH_PRESENCE_URI = Data.CONTENT_URI;
private static final String[] CALLER_ID_PROJECTION = new String[] {
Phone._ID, // 0
Phone.NUMBER, // 1
Phone.LABEL, // 2
Phone.DISPLAY_NAME, // 3
Phone.CONTACT_ID, // 4
Phone.CONTACT_PRESENCE, // 5
Phone.CONTACT_STATUS, // 6
Phone.NORMALIZED_NUMBER, // 7
Contacts.SEND_TO_VOICEMAIL
// 8
};
private static final int PHONE_ID_COLUMN = 0;
private static final int PHONE_NUMBER_COLUMN = 1;
private static final int PHONE_LABEL_COLUMN = 2;
private static final int CONTACT_NAME_COLUMN = 3;
private static final int CONTACT_ID_COLUMN = 4;
private static final int CONTACT_PRESENCE_COLUMN = 5;
private static final int CONTACT_STATUS_COLUMN = 6;
private static final int PHONE_NORMALIZED_NUMBER = 7;
private static final int SEND_TO_VOICEMAIL = 8;
private static final String CALLER_ID_SELECTION_EXACT_MATCH = " Data._ID IN "
+ " (SELECT DISTINCT lookup.data_id " + " FROM "
+ " (SELECT data_id, normalized_number, length(normalized_number) as len "
+ " FROM phone_lookup " + " WHERE normalized_number = ?) AS lookup " + " WHERE "
+ " (lookup.len <= ? AND "
+ " substr(?, ? - lookup.len + 1) = lookup.normalized_number))";
public static String getContactInfoForPhoneNumber(Context context, String number) {
// TODO Auto-generated method stub
String name = null;
boolean isValidNumber = isWellFormedSmsAddress(number);
if (isValidNumber) {
number = PhoneNumberUtils.stripSeparators(number);
}
String normalizedNumber = PhoneNumberUtils.normalizeNumber(number);
// / M:
String matchNumber = PhoneNumberUtils.toCallerIDMinMatch(normalizedNumber);
if (!TextUtils.isEmpty(normalizedNumber) && !TextUtils.isEmpty(matchNumber)) {
String numberLen = String.valueOf(normalizedNumber.length());
String[] args = new String[] {
normalizedNumber, numberLen, normalizedNumber, numberLen
};
Cursor cursor = context.getContentResolver().query(PHONES_WITH_PRESENCE_URI,
CALLER_ID_PROJECTION, CALLER_ID_SELECTION_EXACT_MATCH, args, null);
if (cursor == null) {
return null;
}
try {
if (cursor.moveToFirst()) {
name = cursor.getString(CONTACT_NAME_COLUMN);
}
} finally {
}
if (cursor != null) {
cursor.close();
}
}
return name;
}
public static boolean isWellFormedSmsAddress(String address) {
// MTK-START [mtk04070][120104][ALPS00109412]Solve
// "can't send MMS with MSISDN in international format"
// Merge from ALPS00089029
if (!isDialable(address)) {
return false;
}
// MTK-END [mtk04070][120104][ALPS00109412]Solve
// "can't send MMS with MSISDN in international format"
String networkPortion = PhoneNumberUtils.extractNetworkPortion(address);
return (!(networkPortion.equals("+") || TextUtils.isEmpty(networkPortion)))
&& isDialable(networkPortion);
}
private static boolean isDialable(String address) {
for (int i = 0, count = address.length(); i < count; i++) {
if (!isDialable(address.charAt(i))) {
return false;
}
}
return true;
}
private static boolean isDialable(char c) {
return (c >= '0' && c <= '9') || c == '*' || c == '#' || c == '+' || c == 'N' || c == '('
|| c == ')';
}