public class Utils {
private static final String TAG = “UTILS”;
/**
* 输出Cursor结果集
*
* @param cursor
*/
public static void printCursor(Cursor cursor) {
if (cursor != null && cursor.getCount() > 0) {
int columnCount;
String columnName;
String columnValue; //列值
while (cursor.moveToNext()) {
// 获得行中全部的列的总数
columnCount = cursor.getColumnCount();
for (int i = 0; i < columnCount; i++) {
columnName = cursor.getColumnName(i);
columnValue = cursor.getString(i);
Log.i(TAG, “当前是第” + cursor.getPosition() + “行: ” + columnName + ” = ” + columnValue);
}
}
cursor.moveToPosition(-1); //结束循环
// cursor.close();
}
}
//根据号码取得联系人的名字
public static String getContactName(ContentResolver contentResolver, String address) {
Uri uri = new Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cursor = contentResolver.query(uri, new String[]{Phone.DISPLAY_NAME}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
String cotractName = cursor.getString(0);
cursor.close();
return cotractName;
}
return null;
}
//给定uri找到查询人的id
public static int getContractID(ContentResolver contentResolver, Uri uri) {
Cursor cursor = contentResolver.query(uri, new String[]{“has_phone_number”, “_id”}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int hasPhoneNumber = cursor.getInt(0);
if (hasPhoneNumber > 0) {
int contactID = cursor.getInt(1);
cursor.close();
return contactID;
}
}
return -1; //-1表示返回空
}
//根据联系人id取得联系人的手机号
public static String getContactAddress(ContentResolver contentResolver,int contact_id){
String selection =”contact_id=?”;
String[] sectionArgs = {String.valueOf(contact_id)};
Cursor cursor =contentResolver.query(Phone.CONTENT_URI,new String[]{Phone.NUMBER},selection,sectionArgs,null);
if(cursor!=null &&cursor.moveToFirst()){
String address =cursor.getString(0);
cursor.close();
return address;
}
return null;
}
private static final String TAG = “UTILS”;
/**
* 输出Cursor结果集
*
* @param cursor
*/
public static void printCursor(Cursor cursor) {
if (cursor != null && cursor.getCount() > 0) {
int columnCount;
String columnName;
String columnValue; //列值
while (cursor.moveToNext()) {
// 获得行中全部的列的总数
columnCount = cursor.getColumnCount();
for (int i = 0; i < columnCount; i++) {
columnName = cursor.getColumnName(i);
columnValue = cursor.getString(i);
Log.i(TAG, “当前是第” + cursor.getPosition() + “行: ” + columnName + ” = ” + columnValue);
}
}
cursor.moveToPosition(-1); //结束循环
// cursor.close();
}
}
//根据号码取得联系人的名字
public static String getContactName(ContentResolver contentResolver, String address) {
Uri uri = new Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cursor = contentResolver.query(uri, new String[]{Phone.DISPLAY_NAME}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
String cotractName = cursor.getString(0);
cursor.close();
return cotractName;
}
return null;
}
//给定uri找到查询人的id
public static int getContractID(ContentResolver contentResolver, Uri uri) {
Cursor cursor = contentResolver.query(uri, new String[]{“has_phone_number”, “_id”}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int hasPhoneNumber = cursor.getInt(0);
if (hasPhoneNumber > 0) {
int contactID = cursor.getInt(1);
cursor.close();
return contactID;
}
}
return -1; //-1表示返回空
}
//根据联系人id取得联系人的手机号
public static String getContactAddress(ContentResolver contentResolver,int contact_id){
String selection =”contact_id=?”;
String[] sectionArgs = {String.valueOf(contact_id)};
Cursor cursor =contentResolver.query(Phone.CONTENT_URI,new String[]{Phone.NUMBER},selection,sectionArgs,null);
if(cursor!=null &&cursor.moveToFirst()){
String address =cursor.getString(0);
cursor.close();
return address;
}
return null;
}
解决方案
15
Uri uri = new Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
把上面那句中new刪去看看
5
SDK版本改高一些