Android Question How do you determine the country in which the device is currently located?

fredo

Well-Known Member
Licensed User
Longtime User
For reasons of copyright protection, individual contents should be displayed differently depending on the location of the device.

To determine the current country, it is not enough to use the "GetCountryFromDeviceSetting" function, as this only determines the country setting of the device.

B4X:
public Sub GetCountryFromDeviceSetting As String
    Dim jo As JavaObject
    jo = jo.InitializeStatic("java.util.Locale").RunMethod("getDefault", Null)
    Return jo.RunMethod("getCountry", Null)
End Sub

An identification via geocoding is out of the question, because for data protection reasons the location via GPS identification cannot be used.

Since nowadays almost all smartphones have a SIM card, it should be possible to evaluate the currently active provider.

Does anyone have a useful tip for this?
 

DonManfred

Expert
Licensed User
Longtime User
maybe

https://stackoverflow.com/questions/3659809/where-am-i-get-country

B4X:
/**
 * 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 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.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;
}

Edit to add:

Use this link http://ip-api.com/json ,this will provide all the information as json. From this json you can get the country easily. This site works using your current IP,it automatically detects the IP and sendback details.

Docs http://ip-api.com/docs/api:json Hope it helps.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Use this link http://ip-api.com/json ,this will provide all the information as json. From this json you can get the country easily. This site works using your current IP,it automatically detects the IP and sendback details.

It's probably overkill to mention this, but as your request is about copyright protection: There is a (likely very small) edge case where the user is connected to a VPN, which might place their public internet location in another country. Which, in turn, might make a solution like the one above report a wrong location.

Like I said, probably not worth worrying too much about. Then again, depending on your case, if you were to combine the getUserCountry call with the ip-api response and they both show the same country, you can probably feel very sure about the users' location. And if they differ you might want to ask for some confirmation. All depending on your requirements, obviously.
 
Upvote 0

fredo

Well-Known Member
Licensed User
Longtime User
Thanks @DonManfred and @Sandmann

...edge case where the user is connected to a VPN...
Thanks for mentioning this.

This would affect the network IP. Therefore I had hoped that the approach via the telephony provider would be the most accurate.

Since there is no telephony information for SIM-less devices (e.g. pure Wifi tablet) and vpn can dilute the network location information, I will probably start with the most restrictive solution.
 
Upvote 0
Top