Get country name and code of user [duplicate] - android

This question already has answers here:
Getting telephone US local area code with Android
(10 answers)
Closed 5 years ago.
I want to get user's country from the sim card and it should not be changed if he is in roaming i.e. If I have sim card of India and I am travelling to US then still I should get the country as India only not US.
So what should I use - getNetworkCountryIso() or getSimCountryIso().
I am really confused between two since I have read somewhere that getNetworkCountryIso() returns current country, that means it will be changed if I am in international roaming.

getSimCountryIso
getSimCountryIso depends on the operator to burn the country iso on the SIM and it also supports CDMA networks
getNetworkCountryIso()
Returns the ISO country code equivalent of the current registered operator's MCC (Mobile Country Code).
for more information read TelephonyManager

Related

What is the best way to get user's currency on an android device?

I'm trying to build an app where users can enter prices of products according to their own country's currency (with the currency's symbol attached to an EditText), and then upload the values to a database.
I've already tried the default Locale class but it uses the user's configuration so people like me, using US English on their devices outside the US, can enter only values in US dollars. Also, getting the information from the SIM card is not useful in some cases. How do apps like eBay or even the Play Store get this information? The only thing I can come up with is using the device's GPS. If so, how to extract the information? And if not, what should I do?
Checking the sim card may be a good idea
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Currency.getInstance(new Locale("", telephonyManager.getNetworkCountryIso()));
I'm living in the EU but using US-Locale for my smartphone because I prefer English. The code is giving me the EUR currency

android getLine1Number() returns empty string [duplicate]

This question already has answers here:
MSISDN : Is it a SIM Card Data? Why all The Provided Function (from Blackberry and Android) to fetch MSISDN not reliable?
(3 answers)
Closed 5 years ago.
I want to get the mobile number of users, which will be defaultly taken as username, when I used the following code to get the mobile number, it returns an empty string
TelephonyManager tMgr = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
String mobNo = mPhoneNumber.replace("+", "");
One reason I found is My phone number is unknown under Settings->About Phone->Status->SIM Status. How to rectify this issue without giving any burden to the users.
There are at least some SIM Cards/Operators out there who don't provide numbers - I have seen it and you have seen it. There is probably nothing you can do about it.
See getLine1Number() documentation. It says if its unavailable, it returns null. So #sstn is partially right. You can't do much about it. As far as I can remember this method gets number which is stored in the SIM. Some providers restrict the access and other don't provide it.
However that being said if you are really really pushing to get phone number from network automatically, you can try one more thing. Most of the network providers provide their own API. The API might require the user to connect to internet through the network data and not wi-fi or any other method. Although different providers in different countries can differ on that. You can potentially recognize the network provider using getNetworkOperator(), then use provider specific API to get the number. However not sure if its free. For eg: In Canada, Telus has its API but it requires you to partner with them. Similarly with Rogers etc.
If you don't want to go through that pain, the last option is to ask the user to enter it. If you are worried about its validity then you can always do one time code SMS verification.
Hope it helps.

i need a help to decide which country code ISO should i use for my app

what this code returns in your countries.
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String SIMCountryISO=tm.getNetworkCountryIso();
is it return two charater or three char. please reply with your countries and result
You should be able to find country codes from various resources online. You wouldn't have to go ask the community to do test for you; and doing so is not what StackOverflow is intended for.
Try using a list such as this one to accommodate your needs.
It will return an alpha-2 code.
I am a bit suprised that the API isnt more specific about which type of ISO country code but...
Returns the ISO country code equivalent of the current registered
operator's MCC (Mobile Country Code).
Please also note:
MCC = Mobile Country Codes and are 3 digits.
MNC = Mobile Network Code and can be 2 or 3 digits. The MCC + MNC combination represent a specific carrier.
Country ISO (or country code iso) = Normally ISO_3166-1_alpha-2 and are 2 capital letters. They represent a country (not a language).
Since countries never share MCC codes, you can infer the country from just the MCC code. That is exactly what this method does and will return the 2 character ISO code for the country.
Language ISO codes are completely different and represent a specific language. The combination of Language ISO code + Country ISO code is called a locale. The language iso code is normally lower case.
E.g. en_US

Get current mobile phone number of a user in my android application [duplicate]

This question already has answers here:
MSISDN : Is it a SIM Card Data? Why all The Provided Function (from Blackberry and Android) to fetch MSISDN not reliable?
(3 answers)
Closed 5 years ago.
Without asking the users to input their mobile numbers, I want to get the current mobile phone number associated with that mobile, so that I can send particular messages about deals and offers available at a particular location or country, by getting the mobile phone number when my app is installed at the first time. Is it possible in android? Will there be any problems associated with it, if I'm able to collect mobile phone numbers of users? I would like to more deepely about it. So comments and answers to my question are extremely welcomed. Since I don't know where to start with, any tutorials or related links are also welcomed..
TelephonyManager mManager =(TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = mManager.getLine1Number();
// check if mPhoneNumber is null then there is no number saved in the phone.
and set in manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
and further reading here
hope it helps :)
Spurdow's answer is correct but works only if the number is available in the system. Not all the countries and/or wireless carriers support that. I tested reading phone number, and it gave my T-mobile phone number in USA correctly, whereas returned no number in a friend`s phone in Turkey. So plan your code accordingly(i.e., check a possible null result, ask the phone number to user etc.). For more information:
https://developer.android.com/reference/android/telephony/TelephonyManager.html#getLine1Number%28%29

Mobile app access [duplicate]

This question already has an answer here:
Get MSISDN from the SIM using Android
(1 answer)
Closed 9 years ago.
Can I access the mobile number of a device after installing an app? I can ask from the user for it but need to ensure that it is the correct number that he is inputting. Is there a way? Just need a way to ensure that the app is downloaded on the same mobile number device.
Some SIM providers save the number on the SIM. You can directly extract from SIM without user interaction. Use TelephonyManager as follows:
TelephonyManager teleManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String getSimNumber = teleManager.getLine1Number();
Although, it turns out that there might be a case when SIM is present and still you cannot read the number. Some SIM cards doesn't allow to do so while others just don't have the number in the SIM(since this field is not mandatory). Quoting TelephonyManager.getLine1Number() failing?, Android issue about TelephoneManager and this blog, you'll get to know that the presence of number within the SIM isn’t required for operation and is actually assigned directly by the network itself.
Hope this helps.

Categories

Resources