简体   繁体   English

在Android平板电脑上调用getSimCountryIso()会发生什么?

[英]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() . 在我的Android应用中,为了确定某些功能,我正在使用getSimCountryIso()getNetworkCountryIso()检查设备的ISO国家代码。 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. 其中之一是我的日常驾驶电话,其中装有SIM卡,而其他人则没有SIM卡。 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. 因此,起初,我以为getSimCountryIso()getNetworkCountryIso()总是返回一些值,但是现在我有一个疑问,因为我以前使用过的所有真实设备都拥有SIM卡,并且模拟器可能与真实设备有所不同。

I wonder what will happen when those are called on real Tablets which NEVER HAD SIM cards in them and have only WiFi connections. 我不知道在真正的平板电脑上调用这些产品时会发生什么情况,这些平板电脑中从来没有SIM卡 ,只有WiFi连接。 Can I always expect a certain value? 我总能期待一定的价值吗? Or an exception or null? 还是例外或null?

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() . 我以为您会得到NullPointerException,但是在平板电脑-Pixel C上运行了牛轧糖进行了测试-实际上,我为TelephonyManager.getSimCountryIso()TelephonyManager.getNetworkCountryIso()返回了空字符串。

I had assumed the result would be an NPE because calling PackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) on a tablet will return false . 我以为结果将是NPE,因为在平板电脑上调用PackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)将返回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. 这足以告诉我,您不应该尝试在非电话设备上以这种方式获取SIM卡或网络国家/地区代码。 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. 但是,如果您仍然尝试在此类设备上使用Context.getSystemService()来检索TelephonyManager ,则仍然会得到非null的TelephonyManager它将对提供模拟或网络国家/地区毫无帮助。

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. 我认为,如果您要处理的设备不具备电话系统功能,那么您应该考虑使用GPS位置,或者如果您确实不想侵犯位置权限并且可以容忍该位置,请考虑使用用户设置的语言环境用户设置的语言环境不在用户必需的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM