Search code examples
javaandroidtabletcountry-codes

What will happen when getSimCountryIso() is called on Android Tablets?


In my Android app, in order to determine certain features, I'm checking a device's ISO country code using getSimCountryIso() and getNetworkCountryIso(). Tested this on my real mobile devices and emulators and all returned certain values without any errors.

One of them is my daily driver phone which has a SIM card in it, but the others have no SIM cards. So at first, I thought both getSimCountryIso() and getNetworkCountryIso() always return some values, but now I'm in doubt because all my real devices used to have SIM cards once and emulators may differ from real ones.

I wonder what will happen when those are called on real Tablets which NEVER HAD SIM cards in them and have only WiFi connections. Can I always expect a certain value? Or an exception or null?


Solution

  • I thought you would get a NullPointerException, but having tested it on a tablet - Pixel C, running Nougat - I actually got back empty Strings for both TelephonyManager.getSimCountryIso() and TelephonyManager.getNetworkCountryIso().

    I had assumed the result would be an NPE because calling PackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) on a tablet will return false. That was enough to tell me that you shouldn't be trying to get SIM or network country codes this way on a non-phone device. However, if you try to retrieve the TelephonyManager using Context.getSystemService() on such a device anyway, you still get a non-null TelephonyManager - it just won't be helpful in providing a sim or network country.

    In my opinion, if you are dealing with a device that does not have the Telephony system feature, you ought to consider using GPS location, or the user-set locale if you really don't want to encroach on location permissions and can tolerate the user-set locale not being where the user necessarily is.