Java Code Examples for android.telephony.TelephonyManager#getNetworkCountryIso()

The following examples show how to use android.telephony.TelephonyManager#getNetworkCountryIso() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceUtils.java    From Android-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Return the phone status.
 * <p>Must hold
 * {@code <uses-permission android:name="android.permission.READ_PHONE_STATE" />}</p>
 *
 * @return DeviceId = 99000311726612<br>
 * DeviceSoftwareVersion = 00<br>
 * Line1Number =<br>
 * NetworkCountryIso = cn<br>
 * NetworkOperator = 46003<br>
 * NetworkOperatorName = 中国电信<br>
 * NetworkType = 6<br>
 * PhoneType = 2<br>
 * SimCountryIso = cn<br>
 * SimOperator = 46003<br>
 * SimOperatorName = 中国电信<br>
 * SimSerialNumber = 89860315045710604022<br>
 * SimState = 5<br>
 * SubscriberId(IMSI) = 460030419724900<br>
 * VoiceMailNumber = *86<br>
 */
@SuppressLint("HardwareIds")
@RequiresPermission(READ_PHONE_STATE)
public static String getPhoneStatus() {
    TelephonyManager tm =
            (TelephonyManager) UtilsApp.getApp().getSystemService(Context.TELEPHONY_SERVICE);
    String str = "";
    //noinspection ConstantConditions
    str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
    str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";
    str += "Line1Number = " + tm.getLine1Number() + "\n";
    str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";
    str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";
    str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";
    str += "NetworkType = " + tm.getNetworkType() + "\n";
    str += "PhoneType = " + tm.getPhoneType() + "\n";
    str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";
    str += "SimOperator = " + tm.getSimOperator() + "\n";
    str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";
    str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";
    str += "SimState = " + tm.getSimState() + "\n";
    str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";
    str += "VoiceMailNumber = " + tm.getVoiceMailNumber();
    return str;
}
 
Example 2
Source File: Helper.java    From smartcoins-wallet with MIT License 6 votes vote down vote up
/**
 * Get ISO 3166-1 alpha-2 country code for this device (or null if not available)
 *
 * @param context Context reference to get the TelephonyManager instance from
 * @return country code or null
 */
public static String getDeviceCountry(Context context) {
    try {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        final String simCountry = tm.getSimCountryIso();
        if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
            return simCountry.toLowerCase(Locale.US);
        } else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
            String networkCountry = tm.getNetworkCountryIso();
            if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
                return networkCountry.toLowerCase(Locale.US);
            }
        }
    } catch (Exception e) {
    }
    return null;
}
 
Example 3
Source File: CountryCodePicker.java    From CountryCodePickerProject with Apache License 2.0 6 votes vote down vote up
/**
 * This will detect country from NETWORK info and then load it into CCP.
 *
 * @param loadDefaultWhenFails true if want to reset to default country when network country cannot be detected. if false, then it
 *                             will not change currently selected country
 * @return true if it successfully sets country, false otherwise
 */
public boolean detectNetworkCountry(boolean loadDefaultWhenFails) {
    try {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String networkCountryISO = telephonyManager.getNetworkCountryIso();
        if (networkCountryISO == null || networkCountryISO.isEmpty()) {
            if (loadDefaultWhenFails) {
                resetToDefaultCountry();
            }
            return false;
        }
        setSelectedCountry(CCPCountry.getCountryForNameCodeFromLibraryMasterList(getContext(), getLanguageToApply(), networkCountryISO));
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        if (loadDefaultWhenFails) {
            resetToDefaultCountry();
        }
        return false;
    }
}
 
Example 4
Source File: LocationProvider.java    From Conversations with GNU General Public License v3.0 6 votes vote down vote up
public static String getUserCountry(Context context) {
    try {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        final String simCountry = tm.getSimCountryIso();
        if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
            return simCountry.toUpperCase(Locale.US);
        } else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
            String networkCountry = tm.getNetworkCountryIso();
            if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
                return networkCountry.toUpperCase(Locale.US);
            }
        }
    } catch (Exception e) {
        // fallthrough
    }
    Locale locale = Locale.getDefault();
    return locale.getCountry();
}
 
Example 5
Source File: PhoneUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 获取手机状态信息
 * <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_PHONE_STATE"/>}</p>
 *
 * @return DeviceId(IMEI) = 99000311726612<br>
 * DeviceSoftwareVersion = 00<br>
 * Line1Number =<br>
 * NetworkCountryIso = cn<br>
 * NetworkOperator = 46003<br>
 * NetworkOperatorName = 中国电信<br>
 * NetworkType = 6<br>
 * honeType = 2<br>
 * SimCountryIso = cn<br>
 * SimOperator = 46003<br>
 * SimOperatorName = 中国电信<br>
 * SimSerialNumber = 89860315045710604022<br>
 * SimState = 5<br>
 * SubscriberId(IMSI) = 460030419724900<br>
 * VoiceMailNumber = *86<br>
 */
@SuppressLint("HardwareIds")
public static String getPhoneStatus() {
    TelephonyManager tm = (TelephonyManager) Utils.getContext()
            .getSystemService(Context.TELEPHONY_SERVICE);
    String str = "";
    str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
    str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";
    str += "Line1Number = " + tm.getLine1Number() + "\n";
    str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";
    str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";
    str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";
    str += "NetworkType = " + tm.getNetworkType() + "\n";
    str += "PhoneType = " + tm.getPhoneType() + "\n";
    str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";
    str += "SimOperator = " + tm.getSimOperator() + "\n";
    str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";
    str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";
    str += "SimState = " + tm.getSimState() + "\n";
    str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";
    str += "VoiceMailNumber = " + tm.getVoiceMailNumber() + "\n";
    return str;
}
 
Example 6
Source File: DeviceInfo.java    From Android-Commons with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the ISO 3166-1 alpha-2 country code for the current device
 *
 * @param context a context reference
 * @return the lowercase country code or `null` if not available
 */
public static String getCountryISO2(final Context context) {
	try {
		final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		final String simCountry = tm.getSimCountryIso();

		// if the SIM country code is available
		if (simCountry != null && simCountry.length() == 2) {
			return simCountry.toLowerCase(Locale.US);
		}
		// if the device is not 3G (would be unreliable)
		else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) {
			final String networkCountry = tm.getNetworkCountryIso();

			// if the network country code is available
			if (networkCountry != null && networkCountry.length() == 2) {
				return networkCountry.toLowerCase(Locale.US);
			}
		}
	}
	catch (Exception e) { }

	return null;
}
 
Example 7
Source File: DeviceInfo.java    From shinny-futures-android with GNU General Public License v3.0 6 votes vote down vote up
private String getCountryFromNetwork() {
    try {
        TelephonyManager manager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        if (manager.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) {
            String country = manager.getNetworkCountryIso();
            if (country != null) {
                return country.toUpperCase(Locale.US);
            }
        }
    } catch (Exception e) {
        // Failed to get country from network
        Diagnostics.getLogger().logError("Failed to get country from network", e);
    }
    return null;
}
 
Example 8
Source File: CountryCode.java    From Intra with Apache License 2.0 5 votes vote down vote up
public CountryCode(Context context) {
  TelephonyManager telephonyManager =
      (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

  deviceCountry = countryFromSim(telephonyManager);
  networkCountry = telephonyManager.getNetworkCountryIso();
}
 
Example 9
Source File: Util.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC
 * (Mobile Country Code), or the country code of the default Locale if not available.
 *
 * @param context A context to access the telephony service. If null, only the Locale can be used.
 * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable.
 */
public static String getCountryCode(@Nullable Context context) {
  if (context != null) {
    TelephonyManager telephonyManager =
        (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager != null) {
      String countryCode = telephonyManager.getNetworkCountryIso();
      if (!TextUtils.isEmpty(countryCode)) {
        return toUpperInvariant(countryCode);
      }
    }
  }
  return toUpperInvariant(Locale.getDefault().getCountry());
}
 
Example 10
Source File: AndroidNetworkLibrary.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the ISO country code equivalent of the current MCC.
 */
@CalledByNative
private static String getNetworkCountryIso() {
    TelephonyManager telephonyManager =
            (TelephonyManager) ContextUtils.getApplicationContext().getSystemService(
                    Context.TELEPHONY_SERVICE);
    if (telephonyManager == null) return "";
    return telephonyManager.getNetworkCountryIso();
}
 
Example 11
Source File: Device.java    From unity-ads-android with Apache License 2.0 5 votes vote down vote up
public static String getNetworkCountryISO() {
	if (ClientProperties.getApplicationContext() != null) {
		TelephonyManager tm = (TelephonyManager)ClientProperties.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getNetworkCountryIso();
	}

	return "";
}
 
Example 12
Source File: Util.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC
 * (Mobile Country Code), or the country code of the default Locale if not available.
 *
 * @param context A context to access the telephony service. If null, only the Locale can be used.
 * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable.
 */
public static String getCountryCode(@Nullable Context context) {
  if (context != null) {
    TelephonyManager telephonyManager =
        (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager != null) {
      String countryCode = telephonyManager.getNetworkCountryIso();
      if (!TextUtils.isEmpty(countryCode)) {
        return toUpperInvariant(countryCode);
      }
    }
  }
  return toUpperInvariant(Locale.getDefault().getCountry());
}
 
Example 13
Source File: SystemUtil.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get ISO 3166-1 alpha-2 country code for this device (or null if not available).
 *
 * @return country code or null
 */
@Nullable
public static String getUserCountry() {
	Context context = Application.getAppContext();
	String locale = null;

	final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	final String simCountry = tm.getSimCountryIso();
	if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
		locale = simCountry.toUpperCase(Locale.getDefault());
	}
	else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
		String networkCountry = tm.getNetworkCountryIso();
		if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
			locale = networkCountry.toLowerCase(Locale.getDefault());
		}
	}

	if (locale == null || locale.length() != 2) {
		if (VERSION.SDK_INT >= VERSION_CODES.N) {
			locale = getDefaultLocale24().getCountry();
		}
		else {
			locale = getDefaultLocale23().getCountry();
		}
	}

	return locale;
}
 
Example 14
Source File: Util.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the upper-case ISO 3166-1 alpha-2 country code of the current registered operator's MCC
 * (Mobile Country Code), or the country code of the default Locale if not available.
 *
 * @param context A context to access the telephony service. If null, only the Locale can be used.
 * @return The upper-case ISO 3166-1 alpha-2 country code, or an empty String if unavailable.
 */
public static String getCountryCode(@Nullable Context context) {
  if (context != null) {
    TelephonyManager telephonyManager =
        (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager != null) {
      String countryCode = telephonyManager.getNetworkCountryIso();
      if (!TextUtils.isEmpty(countryCode)) {
        return toUpperInvariant(countryCode);
      }
    }
  }
  return toUpperInvariant(Locale.getDefault().getCountry());
}
 
Example 15
Source File: AndroidNetworkLibrary.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the ISO country code equivalent of the current MCC.
 */
@CalledByNative
private static String getNetworkCountryIso() {
    TelephonyManager telephonyManager =
            (TelephonyManager) ContextUtils.getApplicationContext().getSystemService(
                    Context.TELEPHONY_SERVICE);
    if (telephonyManager == null) return "";
    return telephonyManager.getNetworkCountryIso();
}
 
Example 16
Source File: Devices.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String getDeviceCountry() {
    TelephonyManager tm = (TelephonyManager) AndroidContext.getContext().getSystemService(Context.TELEPHONY_SERVICE);

    String country = tm.getSimCountryIso();

    if (android.text.TextUtils.isEmpty(country)) {
        country = tm.getNetworkCountryIso();
    }

    if (android.text.TextUtils.isEmpty(country)) {
        country = AndroidContext.getContext().getResources().getConfiguration().locale.getCountry();
    }

    return country;
}
 
Example 17
Source File: AssistantActivity.java    From linphone-android with GNU General Public License v3.0 5 votes vote down vote up
DialPlan getDialPlanForCurrentCountry() {
    try {
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        String countryIso = tm.getNetworkCountryIso();
        return getDialPlanFromCountryCode(countryIso);
    } catch (Exception e) {
        Log.e("[Assistant] " + e);
    }
    return null;
}
 
Example 18
Source File: PhoneUtils.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
/**
 * 获取手机状态信息
 * <p>需添加权限<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
 * <p>返回如下
 * <pre>
 * DeviceId(IMEI) = 99000311726612
 * DeviceSoftwareVersion = 00
 * Line1Number =
 * NetworkCountryIso = cn
 * NetworkOperator = 46003
 * NetworkOperatorName = 中国电信
 * NetworkType = 6
 * honeType = 2
 * SimCountryIso = cn
 * SimOperator = 46003
 * SimOperatorName = 中国电信
 * SimSerialNumber = 89860315045710604022
 * SimState = 5
 * SubscriberId(IMSI) = 460030419724900
 * VoiceMailNumber = *86
 * <pre/>
 */
public static String getPhoneStatus(Context context) {
    TelephonyManager tm = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    String str = "";
    str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
    str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";
    str += "Line1Number = " + tm.getLine1Number() + "\n";
    str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";
    str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";
    str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";
    str += "NetworkType = " + tm.getNetworkType() + "\n";
    str += "honeType = " + tm.getPhoneType() + "\n";
    str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";
    str += "SimOperator = " + tm.getSimOperator() + "\n";
    str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";
    str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";
    str += "SimState = " + tm.getSimState() + "\n";
    str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";
    str += "VoiceMailNumber = " + tm.getVoiceMailNumber() + "\n";
    return str;
}
 
Example 19
Source File: DataCollector.java    From evercam-android with GNU Affero General Public License v3.0 4 votes vote down vote up
public static String getCountryCode(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return telephonyManager.getNetworkCountryIso();
}
 
Example 20
Source File: RxDeviceTool.java    From RxTools-master with Apache License 2.0 2 votes vote down vote up
/**
 * 获取ISO标准的国家码,即国际长途区号
 *
 * @param context
 * @return
 */
public static String getNetworkCountryIso(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return tm.getNetworkCountryIso();
}