Reputation: 1902
I am making a project in which i am using the phone contacts . In this app i am matching the number( incoming call number) with the numbers saved in contact list. I have formated the number and it is working over the phones on which the number is saved with this format +919045308261 and its not working over the phones on which the number is saved with this format +91 90 45 308261 The code that I have used is...
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor
.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor phoneCursor = contentResolver.query(
PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?",
new String[] { contact_id }, null);
while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndex(NUMBER));
String formatedphn = PhoneNumberUtils.formatNumber(phoneNumber);
formatedphn = formatedphn.trim();
Log.i("formated number: ", formatedphn);
//here the number is from which the call is incoming
if(number.equals(formatedphn)){
Toast.makeText(getApplicationContext(), "got it", Toast.LENGTH_SHORT).show();
Log.i("Match occurs", "got a hit");
//Log.i("number= ", number);
String contname = phoneCursor.getString(phoneCursor
.getColumnIndex(DISPLAY_NAME));
if(!contname.equals(null)){
Toast.makeText(getApplicationContext(), contname, Toast.LENGTH_SHORT).show();
tts.speak(contname+" calling", TextToSpeech.QUEUE_FLUSH, null);
}
}
Upvotes: 1
Views: 262
Reputation: 536
According to This Thread How to format a phone number using PhoneNumberUtils? you can get formatted Number from +98 91 28 033921 to this 0912 80 33921 and compare with this
Upvotes: 0
Reputation: 12933
String.trim() removes all leading and trailing spaces of a String, but not those in the middle. To remove all whitespaces of a String you can use this regex:
str = str.replaceAll("\\s+","");
Upvotes: 1
Reputation: 12181
Here are some references:
Upvotes: 0
Reputation: 8853
You can do the following thing, compare after removing all the white spaces from the string.
String st = " Hello world";
st = st.trim(); // It should work fine and remove all white spaces.
now compare two strings as you are comparing. All d best
Upvotes: 0