Get Country code - android

I can get the country code in ISO-form using TelephonyManager::getNetworkCountryIso(), as it returns US for USA and such, but how can I get the numeric country dialing code?
I can't seem to find any functions provides me with the data I'm looking for, as I need a function that (for example) returns 01 for USA/Canada, 92 for Pakistan and so on.

Since they're a short list, with a clear mapping to the alpha codes, could you perhaps just store a mapping?
I'm not sure this is what you want though, since the ISO numeric country code for Pakistan is 586 and your question quotes "92", which is the international dialing code for Pakistan. The same principle would work though.

You can get a JSON mapping of ISO2 country code to international dialing code at http://country.io/phone.json. Here's a simple PHP example that makes use of it:
$codes = json_decode(file_get_contents("http://country.io/phone.json"), true);
echo $codes['US'];
// => 1
echo $codes['PK'];
// => 92
See http://country.io/data/ for more related data.

Related

How do I get the local Currency Code for my android app?

I'm integrating my android app with Google analytics v4. I'm in Argentina, so my currency code is "ARS", not "USD". I need to specify the local currency code (of any other country), otherwise it sends wrong information.
For example, the price tag of an article says it costs "9,32 ARS", if I don't specify the currency code it sends "9,32 USD". Thanks
// Send Item
googleTracker.send(new HitBuilders.ItemBuilder()
.setTransactionId(purchase.getOrderId())
.setName(purchase.getPackageName())
.setSku(purchase.getSku())
.setCategory("Coins")
.setPrice(skuDetails.getPriceMicros())
.setQuantity(1)
.setCurrencyCode(????)
.build());
You can try like this
Currency currency = Currency.getInstance(Locale.getDefault());
String currencyCode = currency. getCurrencyCode();
I actually could get it working using this answer here: SOLUTION
Google added to its API a new field which is "price_currency_code", to getSkuDetails, which allows us to get the currency code of a transaction. You must edit SkuDetails.java as this answer shows.
According to the measurement protocol definition for currency code you need to pass a valid ISO 4217 currency code to HitBuilders.ItemBuilder.setCurrencyCode() call. Android provides Currency class that support ISO 4217. You should be able to use it to compose the parameter you need for the setCurrencyCode call.
In android many ways to get local currency.
But the bad thing is, In some cases, we got device-specific issues.
val currency = Currency.getInstance(Locale.getDefault())
currency.currencyCode
currency.symbol
The above code gives the currency code a and symbol, But have an issue, If we change the phone locale, the currency also changed.
We have another way to get local currency, if we change the Phone locale, the local currency is not affected
Currency.getInstance(Locale("",tm.networkCountryIso)).currencyCode.toString()
Currency.getInstance(Locale("",tm.networkCountryIso)).symbol.toString()

How to check given phone number is international or non-international number in android?

My requirement is to find out the message recipient number is an International number or National number.
Is there any android API or any third party library available to implement the same.
I want to use libphonenumber google API[android/externals/libphonenumber] but don't know how to check International and Non-international number.
I don't think telephonyManager.getSimCountryIso() would help you determine 'to which country the call is being made' as it will return your country's ISO.
Moreover, length of ISD codes vary across countries. For some countries it is 1, for some it's 2, for some it's 3 and for others it's 4. So you will need to extract/make 4 different keys of these lengths from the outgoing number as I have shown below:
Say the out going number is +91-XXX-XXX-XXXX. then you'll create 4 keys as:
9 (1 digit key)
91 (2 digit key)
91X (3 digit key)
91XX (4 digit key)
Now check if any of these 4 keys is present in this list: ISO List .
[EDIT: Alternative Solution]
Again, if you only need to determine if the call being made is international or not then you can simply check for below condition:
if(outgoing-number `startswith` "00" || outgoing-number does not `startswith` your "country's- ISD-code") {
//it's an international call;
} else {
//it's a domestic call;
}
Rather use the google library which is used internally in Android as well.
http://code.google.com/p/libphonenumber/

Correct Value(s) to store in TAG_GPS_PROCESSING_METHOD

Thanks for reading this question. I am sure the experts on this site will be able to provide the help I need.
I am trying to write an app which allows users to edit the exif information of the photos on their Android Phone.
As a part of improved user experience, I want to apply data validation where ever possible.
For the Exif Tag - TAG_GPS_PROCESSING_METHOD I am not able to apply the validation correctly.
Here is the part of code that I have applied :
String strGPSProc = etGPSProc.getText().toString();
if(strGPSProc.equalsIgnoreCase("GPS") || strGPSProc.equalsIgnoreCase("CELLID") || strGPSProc.equalsIgnoreCase("WLAN") || strGPSProc.equalsIgnoreCase("MANUAL") ) {
returnValue = true;
}else {
returnValue=false;
showToast("Incorrect value for GPS Processing Method. Correct value options are GPS, CELLID, WLAN or MANUAL.");
etGPSProc.requestFocus();
}
This code checks if the value entered in the EditText meant for GPSProcessingMethod, has any one of the four prescribed value as described in the documentation of EXIF.
But when I try to save this using setAttribute() and saveAttributes() functions, a non catch-able exception appears in logcat.
Unsupported encoding for GPSProcessingMethod
I understand from Exif Documentation that values for GPSProcessingMethod needs to be stored with some header information.
I need some expert advise on how to implement this correctly, with out using any other 3rd part classes.
Accoridng to the Exif specification:
GPSProcessingMethod
A character string recording the name of the method used for location finding. The first byte indicates the character
code used (Table 6、Table 7), and this is followed by the name of the method. Since the Type is not ASCII, NULL
termination is not necessary
Atually, Table 6 lists the character codes as 8 byte sequences, so the above should probably read "The first bytes indicate...". Anyway, the character code designation for ASCII is defined as 41.H, 53.H, 43.H, 49.H, 49.H, 00.H, 00.H, 00.H., Unicode is (unsurprisingly) 55.H, 4E.H, 49.H, 43.H, 4F.H, 44.H, 45.H, 00.H. I guess these should be all you need.
Hope that helps.
EDIT:
Just discovered that ExifInterface.setAttribute() only supports String values... You could try encoding the value at the beginning of your string, but I doubt that would work. Sounds like the encoding should be handled by the setAttribute() or saveAttributes() method. Could it be a bug in the API? I had a look at the source code, but the actual writing of values is done by native code so I stopped digging further.

Get country code from numbers that are different length

We know that there are numbers with different length. In Europe we mostly have 9 digits numbers plus country code.
In North America we often find 10 digits numbers.
I am trying to get my head around an idea how to get a country code from a number that may be of different length.
Any ideas? Maybe you know some working libs that can do it?
The key facts:
The country code is always at the start of the number, so it is easy to find no matter the length of the number.
There is no overlap, as #Luis points out.
A (looks pretty) complete list of country codes is give here. If you sort them by length (shortest first) and run through the list comparing the first n digits with the list entries you will get the answer.
However, if you look at the list you wall see that there are various groups of codes. A more intelligent approach would note that:
All numbers beginning with 1 are US, Canada or other US related places in which case the next three digits tell you which.
7 is Khazakstan
Apart from 20, all country codes beginning with 2 are three digits.
and so on ...
Country codes are parsed left-to-right with deterministic endpoints similar to the idea of Huffman coding. ie, if you see a 1 first, stop, it's the US/Canada/related territories. If you see most other numbers besides 7 (Russia/Kazakhstan), keep going. Some of those numbers may terminate on the second value.
The list of country codes is here: http://www.howtocallabroad.com/codes.html
It should be trivial for you to take this and write your own string parser of a phone number in order to determine which country code is present.
(don't forget that if these are numbers from within a particular country, you also have to take that country's exit code into account, which is also on the page I linked)
Edit: Oh, I guess luis covered it. But Jakob is incorrect in his comment about Barbados. Barbados is the same country code as the US; the 246 is its local "area code" within the US/Canada's country code.
I assume that you are talking about phone number country codes. Country codes are defined by the ITU ( https://en.wikipedia.org/wiki/List_of_country_calling_codes ). The country codes can be 1, 2 or 3 digits. Your only alternative is to have a list of all country codes and parse it from there. Note that there is no overlap; for instance, +44 belongs to the UK, and no country starts with just 4.
UPDATE: The North American Area has 4 digit prefixes, not 1, composed of +1 and a NPA of 3 digit (http://en.wikipedia.org/wiki/North_American_Numbering_Plan). The same rule applies though, in that +1-NPA cannot be repeated. Barbados seems to be +1246, but no other country or region can start with +1246. You can get the list of all NPA from http://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes

check mobile number valid or not through country code

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.

Categories

Resources