我发现检索MCC和MNC的唯一方法是覆盖一个活动的onConfigurationChanged方法,如下:
public void onConfigurationChanged(Configuration config)
{
super.onConfigurationChanged(config);
DeviceData.MCC = "" + config.mcc;
DeviceData.MNC = "" +config.mnc;
}
Run Code Online (Sandbox Code Playgroud)
但是,一旦应用程序启动,我就需要这些数据,并且无法等待用户切换手机的方向或等效触发此方法.有没有更好的方法来访问当前的Configuration对象?
小智 90
该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));
}
}
Run Code Online (Sandbox Code Playgroud)
小智 19
你知道有两个MCC/MNC用于活动手机吗?(一个是Sim卡的国家代码和运营商ID,另一个是正在使用的网络/蜂窝塔.)
如果getResources().getConfiguration().mcc在飞行模式下不是空的,那就是Sim值TelephonyManager.getSimOperator(),而不是tower值TelephonyManager.getNetworkOperator().
我不知道OP想要哪个,但如果getConfiguration真的是Sim值,那么答案3会给他不同于原始代码的结果.
getResources().getConfiguration().mcc是一个糟糕的选择,因为它返回一个整数,因此会损害诸如01, 或 之类的有效值044。显然整数不是一个好的选择。
查看Mobile_Network_Code 中的详细信息
更新:在澳大利亚,我们在这里验证了一个错误的案例。在getNetworkOperator从返回不同的值getSimOperator,其中后者是正确的。
请参阅 Android 文档中的详细信息:TelephonyManager
| 归档时间: |
|
| 查看次数: |
55289 次 |
| 最近记录: |