I am building an android app for blocking calls. Now when i am asking the user for contact that he/she wants to block then that number may or may not contain that country's code, while when i will receive a call from that person it will surely have the country code. For example,
in India one can store its number in three different ways:
1) 90331xxxxx
2) 090331xxxxx
3) +9190331xxxxx
Now if user added a contact number which was stored in first way then how could i compare it with the incoming number because the incoming number will be surely having country code.
Also the length of all mobiles number across the world is different. So, how should i compare this numbers.
You should use https://code.google.com/p/libphonenumber/. This has lot of features that you need.
https://code.google.com/p/libphonenumber/#Quick_Examples states how you can use this library to parse numbers and get country specific number, so basically you need to just get the local number using this library and then use that for your comparison.
String swissNumberStr = "044 668 18 00"
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH");
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
boolean isValid = phoneUtil.isValidNumber(swissNumberProto); // returns true
// Produces "+41 44 668 18 00"
System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.INTERNATIONAL));
// Produces "044 668 18 00"
System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.NATIONAL));
// Produces "+41446681800"
System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.E164));
Related
I'm developing sms APP and want to receive sms from the specific numbers. But number can be changed sometime with country code as +923201234567 or sometime without country code 03201234567 how I can compare number from database? because don't know in which format number is saved in database(with country code or without country code)
public boolean isMember(String phone, long id){
String query = "SELECT * from members where phone = ? AND active = 1 AND gid = ?";
Cursor c = dbActions.rawQuery(query, new String[]{String.valueOf(phone), String.valueOf(id)});
return c.moveToFirst();
}
Suppose if the number is saved in database without country code 03201234567 then my requirement is to get true if I compare it with country code. +923201234567. Country code could be changed.
PhoneNumberUtils.compare(); is not useful because it not compare with database.
If you can't acquire the correct information always; then you need to look into heuristics.
Meaning: you could write your own comparisons; and when you encounter two numbers like:
03201234567
+923201234567
you can figure: their "tail" is equal; the only difference is that the first one starts with 0 (so no country code) and the second one with +92. So it might be reasonable to declare those two numbers to be equal.
So a "solution" would do things like "normalize" your input (remove all non-digit content; except for leading + signs); and to then make such "tail-bound" comparisons.
If that is "too" fuzzy; I guess then you should step back and describe the requirement that you actually try to resolve here. Why are you comparing numbers; and what do you intend to do with the output of that comparison?!
Normalize all of the phone numbers into the same format before you put them into the database. That way you can just do a normal db search.
The other thing I've done for phone numbers is to convert all letters into the appropriate number, then remove all non digits, then just compare the last 7 digits.
I am developing an app and I need to compare the phone numbers of the device contact list to a phone number list in a database, but the phone numbers can be written in different forms like it’s described in Wikipedia:
Number structure for networks
Country Code - cc = 1 to 3 digits
Identification Code = x = 1 to 4 digits
Subscriber Number = maximum = 15 − (cc + x) = 8 to 11 digits
International public telecommunication number for networks (maximum 15 digits)
So in Brazil the phone number can be written like:
Subscriber Number 99999-9999
Identification Code + Subscriber Number 67 99999-9999
Country Code + Identification Code + Subscriber Number 55 67
99999-9999 or
+55 67 99999-9999
And in other countries the phone number can be written in different forms and can have different number patterns.
How can I compare the numbers from the device to the ones in the database like Telegram and Whatsapp do? Do I have to create a code to handle each country individually?
I am using libphonenumber for Android and libPhoneNumber-iOS for Swift and they work really well.
For getting the area code of the number a use getLengthOfGeographicalAreaCode like this:
String nationalSignificantNumber = phoneUtil.getNationalSignificantNumber(number);
int nationalDestinationCodeLength = phoneUtil.getLengthOfNationalDestinationCode(number);
if (nationalDestinationCodeLength > 0) {
nationalDestinationCode = nationalSignificantNumber.substring(0, nationalDestinationCodeLength);
}
For comparing two number I use isNumberMatch method. It works fine.
I am allowing user to register with mobile number only and i wanna validate number in app without using internet, what i wanna do is that get valid phone number length of country selected by user , validate it with length and formatting of number in that country's format.
for e.g. length of India's phone number is 10 and format is 0**********
Please tell me how to do that.
Add Following Code.
In gradle
compile 'com.googlecode.libphonenumber:libphonenumber:8.5.2'
In Activity class
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(countryCode));
String exampleNumber= String.valueOf(phoneNumberUtil.getExampleNumber(isoCode).getNationalNumber());
int phoneLength=exampleNumber.length();
editTextLoginPhone.setFilters( new InputFilter[] {
new InputFilter.LengthFilter(phoneLength)});
Hope this code will help you.
if you not want to use the internet of checking the number valid or not , you can do programmatically though its long but solution can be this only .
Go to link : https://en.wikipedia.org/wiki/List_of_mobile_phone_number_series_by_country
and do programmatically the validation with conditions.
You can try this code for Indian Phone numbers:-
private static boolean isValidPhoneNumber(String mobile) {
String regEx = "^[0-9]{10}$";
return mobile.matches(regEx);
}
you should force user to enter the country code and you can validate the phone number also just go through the links below
https://github.com/googlei18n/libphonenumber
and
List of phone number country codes
I'm working on a project that is similar to Message in android.
There are 2 types of number : sms number and phone number.
For example : I always receive sms with number is: +84973612399. But her phone number is 0973612399. How can I know that 2 numbers only belongs to a person?
Thanks.
Use PhoneNumberUtils.compare to compare both numbers.
Example
//Compare phone numbers a and b, return true if they're identical enough for caller ID purposes.
if (PhoneNumberUtils.compare("+84973612399", "0973612399")) {
Log.d(TAG, "Both are identicaly same");
} else {
Log.d(TAG, "Doesn't match");
}
Result is
Both are identicaly same
I have contact like "+919672525253".Now i extract the country code like "91" from that number.Now if number is like "9672525253" and if i extract the country code then it will give me "967".So after extracting the country code how can i check that remaining number is valid mobile number for that country code or not?
EDIT
If any body know the mobile number length country wise then also i can solve this problem.like in india 10 digits.
You pretty much can't. For example in the US mobile numbers and landline numbers are indistinguishable, they have normal area codes just like landline numbers. Even if it were possible every country does it differently and it is also constantly changing as numbers run out new prefixes are added and things change and their is no pattern you could match against or database you could do a lookup against.
Take a look at libPhoneNumber (bundled in ICS) which can help validating a phone number (see PhoneNumberUtils).
There's a MobileType you can get after validation but as stated in the source and by Ben, in some region this will not work.
EDIT:
Some validation code (here we need to check the phone is a valid one assuming it's a french one):
boolean isValid = false;
PhoneNumber number = null;
try {
number = this.phoneUtil.parse(phone, "FR"); // phone is number in internationnal format "+xxxxxx"
isValid = this.phoneUtil.isValidNumber(number);
} catch (final NumberParseException e) {
// ...
}
isValid // is the phone number valid according to the library?
this.phoneUtil.getRegionCodeForNumber(number); // this gets the country code of the phone as found by the library (for example "US", "CH", "GB", ...)
This works for us but you'll need to try it to see if it suit your need.