[英]Android Get contact name from phone number
我正在嘗試獲取有關使用電話號碼的聯系人的詳細信息,一切正常,但是當使用某些特殊字符保存聯系人號碼時,我無法獲取以下的聯系人詳細信息是我的代碼:
//function called
getContactName("+11234567890");
並且在聯系人中保存的電話號碼是(+1(123)456-789)
//function
public String getContactName(String number) {
String name;
if(number != null && !number.equals("")){
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor cursor = _context.getContentResolver().query(contactUri, projection, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
cursor.close();
}
}
return name;
}
使用波紋管功能從電話號碼中獲取姓名。 我最近測試過。 工作正常。 因為電話查找將替換電話號碼中的所有特殊字符。
例如,我用名稱Gaurav保存了新的數字0 + 0141(12-23)。 然后我調用函數getContactName(01411223),它將返回名稱Gauav。
請使用以下功能,如果無法使用,請通知我。
public static String getContactName(Context context, String phoneNumber) {
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return null;
}
String contactName = null;
if(cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
return contactName;
}
您可以使用PhoneNumberUtils.stripSeparators(String)
或者你可以使用正則表達式
filterNum = filterNum.replaceAll("[^0-9]+", "");
這將刪除所有不在0...9
范圍內的字符。 我認為這樣會更容易。
這是文檔 。
您可以使用replaceAll("\\\\D+","");
從字符串中刪除所有非數字字符 replaceAll("\\\\D+","");
,看看,讓我知道它是否有效!
public String getContactName(String number) {
number = number.replaceAll("\\D+","");
String name;
if(number != null && !number.equals("")){
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor cursor = _context.getContentResolver().query(contactUri, projection, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
cursor.close();
}
}
return name;
}
可能這段代碼會有所幫助
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.