I'm trying to get phone number from contact and if it has country code, separate it from mobile number. We've two fields CountryCode and MobileNumber.
There are 4 different scenarios i like to handle
(+CountryCode)(MobileNumber) : I like to get both
(CountryCode)(MobileNumber) : I like to get both
(0)(MobileNumber) : I like to get MobileNumber
(MobileNumber) : I like to get MobileNumber
I've tried using Google libphonenumber, but it's failing in 4th scenario.
For example,In case if my contact is like this 8109112345. I'll get country code as 81 and mobileNumber as 09112345 because Country code of Japan is +81.
This is how i'm using the library.
int countryCode = phoneNumberUtil.extractCountryCode(new StringBuilder(contact), new StringBuilder(""));
String regionCode = phoneNumberUtil.getRegionCodeForCountryCode(countryCode);
final Phonenumber.PhoneNumber phoneNumber = util.parse(contact, regionCode);
Is there any other library which does this or any other way this can be achieved ?
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 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 want to display country flags with respect to phone-numbers in my contact lists like rebtel(Screen shot shown below).
Parse the phone number and get the international prefix, then you can use Wikipedia: List_of_country_calling_codes to get the country. You can implement a Map with key being the international prefix and key the country name.
Here I am giving you the logic part,
You can get the ISD code and flag image from this link http://en.wikipedia.org/wiki/List_of_country_calling_codes#Alphabetical_listing_by_country_or_region
You need to make two Array variables,
String [] countryCode = new String { "+91","+92", ... };
int [] country = new int { R.drawable.91.png, R.drawable.92.png,.... };
// Considering here 91.png is india's map
Now get the phone number from contact list , suppose you have +91987654321. So remove first + sign and compare 91 to all the resources images. When match found, put that image with the contact.
You can use this library for your Requirement.
Go to detail you can show country flag and country code with use of this.
Assuming that I don't know my current location, how to move MapView to country by it's name?
What I'm trying to do is display user's country by country code in locale.
There isn't such a table in android with a link between country and geolocation that I'am aware of.
I believe the best approach is to get a list of all capital cities with latitude and longitude (you can get them here : list of capital cities) or a list of countries (you can get them from here : list of countries) and put it in a table in your application.
good luck.
Yes you can display User's country by Country Code.
As we have facility to get the CountryCode through Android Telephony Manager Api so.
1)You have to use Key Value pairs concept, Key as CountryCode and Value as Country.
like the following code:
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
manager.getNetworkCountryIso();
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.