Java 你如何在Android中获得手机的MCC和MNC?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/890366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do you get the phone's MCC and MNC in Android?
提问by
The only way I've found of retrieving MCC and MNC is by overriding an activity's onConfigurationChanged method, as such:
我发现检索 MCC 和 MNC 的唯一方法是覆盖活动的 onConfigurationChanged 方法,如下所示:
public void onConfigurationChanged(Configuration config)
{
super.onConfigurationChanged(config);
DeviceData.MCC = "" + config.mcc;
DeviceData.MNC = "" +config.mnc;
}
However, I need this data as soon as the app starts and can't wait for the user to switch the phone's orientation or equivalent to trigger this method. Is there a better way to access the current Configuration object?
但是,一旦应用程序启动,我就需要这些数据,并且不能等待用户切换手机的方向或等效来触发此方法。有没有更好的方法来访问当前的 Configuration 对象?
采纳答案by jargonjustin
The TelephonyManagerhas a method to return the MCC+MNC as a String (getNetworkOperator()) which will do you what you want. You can get access it via:
该TelephonyManager有返回MCC + MNC作为一个字符串的方法(getNetworkOperator() ),它会做你想要的东西。您可以通过以下方式访问它:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String networkOperator = tel.getNetworkOperator();
if (!TextUtils.isEmpty(networkOperator)) {
int mcc = Integer.parseInt(networkOperator.substring(0, 3));
int mnc = Integer.parseInt(networkOperator.substring(3));
}
}
回答by jargonjustin
You can access the current configuration by getResources().getConfiguration()
does the trick.
您可以通过getResources().getConfiguration()
操作来访问当前配置。
回答by Artem
Okay, it turns out that the getResources().getConfiguration().mcc trick is likely better for most purposes, because with the other one if the person puts their phone in airplane mode or otherwise uses Wi-Fi, then it returns an empty MCC.
好吧,事实证明 getResources().getConfiguration().mcc 技巧在大多数情况下可能更好,因为如果人们将手机置于飞行模式或以其他方式使用 Wi-Fi,那么它会返回一个空的中冶。
回答by danS
You do know there are two MCC/MNC's for an active phone? (One is the country code and carrier id for the Sim card, the other is for the network/cell tower in use.)
您知道一部有源电话有两个 MCC/MNC 吗?(一个是 SIM 卡的国家代码和运营商 ID,另一个是正在使用的网络/手机信号塔。)
If the getResources().getConfiguration().mcc
is not empty in airplane mode, it's the Sim
value TelephonyManager.getSimOperator()
, not the tower
value TelephonyManager.getNetworkOperator()
.
如果getResources().getConfiguration().mcc
在飞行模式下不为空,则是Sim
value TelephonyManager.getSimOperator()
,而不是tower
value TelephonyManager.getNetworkOperator()
。
I don't know which the OP wants, but Answer 3 will give him different results than his original code if the getConfiguration
is really the Sim
values.
我不知道 OP 想要哪个,但是如果getConfiguration
确实是Sim
值,答案 3 会给他与原始代码不同的结果。
回答by bruno.braga
getResources().getConfiguration().mcc
is a bad choice because it returns an integer, hence compromising valid values such as 01
, or 044
. Clearly integer is not a good option for this.
getResources().getConfiguration().mcc
是一个糟糕的选择,因为它返回一个整数,因此会损害诸如01
, 或 之类的有效值044
。显然整数不是一个好的选择。
See details in Mobile_Network_Code
查看Mobile_Network_Code 中的详细信息
Update: in Australia, we verified a wrong case here. The getNetworkOperator
returns different value from getSimOperator
, where the latter is correct.
更新:在澳大利亚,我们在这里验证了一个错误的案例。在getNetworkOperator
从返回不同的值getSimOperator
,其中后者是正确的。
See details in Android doc: TelephonyManager
请参阅 Android 文档中的详细信息:TelephonyManager
回答by kakopappa
I found out that network operator sometimes can be like 65@5
when not connected to the operator (service unavailable) even if there is a a SIM card inserted. This happened on Samsung S2 running Android 4.1.2.
我发现网络运营商有时会像65@5
没有连接到运营商(服务不可用),即使插入了 SIM 卡。这发生在运行 Android 4.1.2 的三星 S2 上。
So you have to be careful when converting to Int.
所以在转换为 Int 时要小心。
int mcc = Integer.parseInt(networkOperator.substring(0, 3));
回答by Akash pasupathi
this is updated. use this
这是更新。用这个
TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String networkOperator = tel.getSimOperator();
System.out.println("************mnc,mcc"+networkOperator);
if (!TextUtils.isEmpty(networkOperator)) {
mcc = networkOperator.substring(0, 3);
mnc = networkOperator.substring(3);System.out.println("************mnc,mcc"+mnc+mcc);
}mnc_mcc.setText("************mnc,mcc"+mnc+","+mcc);
}