【发布时间】:2016-08-02 16:37:52
【问题描述】:
我试图在SIM LOADED 状态后获取mcc 和mnc,以检查SIM 卡是否在没有READ PHONE STATE 权限的情况下发生更改,以禁用某些网络的应用请求以及在一些用户不想要的国家/地区。
由于getSimOperator() 可能会根据当前运营商而改变(例如,当用户是否在漫游时)我决定使用getNetworkOperator()。
尽管此方法可以返回 null,即使 SIM 是 LOADED 并且可能返回不同的结果,例如一张只有 GSM 连接的 lycamobile 卡给了我mnc = 01,当我取出 SIM 卡并重新放入时,它给了我mnc = 04。
有人知道为什么 mnc 对getNetworkOperator() 给出不同的结果吗?对于这种情况,getNetworkOperator() 或 getSimOperator() 哪种方法更好?
另外,我不能使用 getResources().getConfiguration().mcc,因为它提供了一个 int 数字,可能会在例如之前删除 0。给出4 而不是04。
这是我检查 SIM 状态变化的代码:
@Override
public void onReceive(final Context context, Intent intent) {
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
String ss = extras.getString(EXTRAS_SIM_STATUS);
if (ss != null && (ss.equals("LOADED"))) {
TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && hasPermissions(READ_PHONE_STATE)) {
//here I get the imsi
}else{
L.d("NetworkOperator result %s", telephonyManager.getNetworkOperator());
//saving in shared preferences in order to check if the sim is allowed or not
//this is also called on application onCreate() so I can check the current SIM
}
}
}
}
}
PS:我使用的 SIM 卡只有 GSM 连接。我还尝试了另一张卡(具有 4g 功能),一切正常,mnc 与沃达丰卡的01 相同。
【问题讨论】:
-
我认为你有这个:
Since getSimOperator() may change according to the current carrier (e.g. when the user is on roaming or not) I decided to use the getNetworkOperator().倒退。 getSimOperator 应该随 SIM 卡而改变——但在漫游等时不会改变。 -
我虽然是相反的方式,但要在模拟器上测试,然后我会回复你,谢谢;)
-
谢谢,就是你说的,getSimOperator()来自simcard
标签: android gsm mobile-country-code