Reliable method to get the country the user is in?

后端 未结 5 1007
情话喂你
情话喂你 2020-12-05 03:19

I usually get the country from the device\'s language. It works but now I have to recognize Brazil. And most of the devices only have portuguese (pt_PT), and no portuguese (

5条回答
  •  无人及你
    2020-12-05 03:43

    TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    mTelephonyManager.getNetworkCountryIso();
    

    As answered by Eric, the above code is the best approach. It is also worth noting that,

    mTelephonyManager.getSimCountryIso()
    

    should not be used as this would indicate the home country of the SIM provider (E.g. A Vodaphone UK SIM would return "gb", A Vodaphone Germany SIM would return "de") and not the current location (Country) of the device. This difference is significant when the user is roaming.

提交回复
热议问题