I am not able to get Exact Country Where User is Using my Android Application.
I tried Many Ways but not Getting the exact answer.
Firstly i Used TelephonyManager Like this but not getting exact Answer
TelephonyManager tMgr = (TelephonyManager) CheckCountry.this
.getSystemService(Context.TELEPHONY_SERVICE);
phNo = tMgr.getLine1Number();
Then i used this But it return value by getting From My Mobile Settings
String locale = getApplicationContext().getResources()
.getConfiguration().locale.getCountry();
Have AnyOne Exact Solution by which I can get Country name then Let me Know
Well I found a better solution for this. By using This api I can get All the details about User Current Location And Country. Here is the API
API to Get Country
if connected to United State then "US"
TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();
or
TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
Related
This question already has answers here:
Where am I? - Get country
(10 answers)
How can I get my Android device country code without using GPS?
(10 answers)
Closed last month.
The community is reviewing whether to reopen this question as of 11 days ago.
How can I get current region's code like (i.e. US, UK) selected in Android device.
I don't want to get region/country code from locale because it only returns country code from language selected. Using locale:
String cCode = Locale.getDefault().getCountry();
Whenever user explicitly updates region/country, it must reflect in the app. I need code to get current selected region/country of Android device.
I was working on the same. This helped me:
Locale current = getResources().getConfiguration().locale;
To get the country code, you can use the getCountry() method of the Locale class as shown in the code.
To get the full region/country name, you can use the getDisplayCountry() method of the Locale class as shown in the code above. This will return the full name of the country for the device's current locale in the language of the device.
Locale currentLocale = Locale.getDefault();
String countryCode = currentLocale.getCountry();
String regionName = currentLocale.getDisplayCountry();
For example, if the device's current locale is set to United States, the country code will be "US" and the region name will be "United States". If the device's current locale is set to France, the country code will be "FR" and the region name will be "France". So, this will return both the country name and region name.
you can use the getNetworkCountryIso method of the TelephonyManager class to get the country code of the device's current network
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso();
I hope this will work ..
TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();
Try this:
String countryCode = Locale.getDefault().getCountry();
String languageCode = Locale.getDefault().getLanguage();
I'm updating some code to use the Multi SIM support that was added to Android in 5.1. We examine the SIM operator (or the MCC+MNC combo) a lot in our app, so the code I'm writing involves migrating from reliance on TelephonyManager.getSimOperator() to getting SubscriptionInfos from the SubscriptionManager, each of which gives access to the MCC and MNC for a SIM.
//old way – can only access one SIM
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String simOperator = telephonyManager.getSimOperator();
//new way - gives access to all SIMs
SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> subInfoList = subscriptionManager.getActiveSubscriptionInfoList();
for(SubscriptionInfo info : subInfoList) {
int mcc = info.getMcc();
int mnc = info.getMnc();
//do some stuff...
}
I'm finding that because it's returning ints for MCC and MNC, you lose leading zeros that are in some of the MNCs. To see some examples, check out the table here. When you concat MCC+MNC you get the SIM operator, so it is important to keep the zeros that are often present. For example, NEXTEL in Mexico has a SIM operator value of "334090," but because the SubscriptionInfo returns ints for MCC and MNC, you get the values 334 and 90, respectively.
Given this issue, how can you reliably link a SubscriptionInfo's MCC+MNC pair to a particular SIM operator in a list of known SIM operators? Is this an oversight in the design of this API? Is there any other way to get the SIM operator for every SIM card that's in the multi-SIM phone?
MCC are always 3 digits, the rest is MNC, which as you correctly said, may have 2 or 3 digits.
If you parse the int to String, you can just get sub string of it and process the info as required.
Also it's a hidden method, but you can access it by reflection, to get the info you want in String format, this exists at least from Android 6 and Android 7
public String getSimOperator(int subId)
But I think SubscriptionManager way is safer since it's open API and for the moment you don't need to bother with Android versions handling it differently.
The best way to achieve this is to read IMSI from the SIM card. If you are reading the MNC+MNC directly and your user is on roaming or connected to another network then the values returned won't be reliable.
E.g. If your user is NEXTEL customer but is travelling or due to network connection issues connects to another network, lets say Telcel, then the value returned from MCC+MNC will be from Telcel.
In order to reliably match the operator of current SIM to predefined list, read IMSI value from SIM card using getSubscriptionId() from Telephony manager.
IMSI starts with MCC+MNC of the operator. You can easily check if MCC+MNC matches as follows
final String mccMncToCompare = "334090"; //You can also use Integer instead of string. You just have to perform type conversion
final TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
final String imsi= tm.getSubscriberId();
final boolean mccMncMatched = imsi.startsWith(mccMncToCompare);
I am just trying to get my carrier name.
The issue while trying to use:
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE));
String carrierName = manager.getNetworkOperatorName();
That is working great unless you are a MVNO (definition here: http://en.wikipedia.org/wiki/Mobile_virtual_network_operator)
For instance, in Belgium, we have the "Base" carrier that rent the network to some MVNO, like Toledo, Allo RTL, Mobile Vikings, etc...
In my notification bar, when using such virtual operators, I get their real names, but the code above just give me "Base" as String, and not the one I want.
I hope you understand what I mean.
I don't have a MVNO SIM card but the getSimOperatorName() method of the TelephonyManager might be the method you are looking for.
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String carrierName = manager.geSimOperatorName();
I have a MVNE SIM card in Germany and the method returns an empty String, but the getNetworkOperatorName() method returns the right Operator for my SIM card.
I want to get a unique name or id of the android phone by using the following :
String uniqueID = android.provider.Settings.Secure.getString(getContentResolver(),
android.provider.Settings.Secure.ANDROID_ID);
but it is return null , i do not know what is the problem .
Any help?
Personally, I wouldn't use ANDROID_ID. It's been known to be null sometimes and it can change after a factory reset. The device id on the other hand is always non-null (in my experience at least), and unique.
You can add this code to your Activity to retrieve the unique device id of the phone:
TelephonyManager tm =
(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String device = tm.getDeviceId();
Try it with context,
String m_androidId = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
I need to detect the MCC of the country in which the GSM/UMTS wireless modem is currently now.
Based on GSM networks
You need to use getSimCountryIso() and getNetworkCountryIso() from the TelephonyManager
Returns the ISO country code equivalent for the SIM provider's country code.
Based on WIFI you use a Ip to Country database
You have also the option to use the Geocoder class based on the location
First get the MCC/MNC:
TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String networkOperator = tel.getNetworkOperator();
if (networkOperator != null) {
int mcc = Integer.parseInt(networkOperator.substring(0, 3));
int mnc = Integer.parseInt(networkOperator.substring(3));
}
Then, based on that, you can get the number corresponding to the selected MCC. There are plenty of lists on the internet, for example this one on Wikipedia