问题
I know that using TelephonyManager
we can get MNC and MCC of our network provider,
TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String networkOperator = tel.getNetworkOperator();
if (networkOperator != null) {
int mcc = Integer.parseInt(networkOperator.substring(0, 3));
int mnc = Integer.parseInt(networkOperator.substring(3));
}
But I was able to get MNC and MCC data of only primary sim. I would like to know is there a way to fetch mnc,mcc,lac and cellid's of Secondary sim of a device in android.
回答1:
Before API 22, it will be hard to achieve what you want.
I believe that before API 22, dual SIM was not originally supported by Android. So, each Mobile vendor should have its own implementation. Maybe, you should get their APIs/SDKs and include them to your project. Only this way, you will have access to their APIs.
From API22 onward, I think you can use SubscriptionManager
int mccSlot1 = -1;
int mccSlot2 = -1;
int mncSlot1 = -1;
int mncSlot2 = -1;
SubscriptionManager subManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
if(subManager.getActiveSubscriptionInfoCount() >= 1) {
mccSlot1 = subManager.getActiveSubscriptionInfo(0).getMcc();
mncSlot1 = subManager.getActiveSubscriptionInfo(0).getMnc();
}
if(subManager.getActiveSubscriptionInfoCount() >= 2) {
mccSlot2 = subManager.getActiveSubscriptionInfo(1).getMcc();
mncSlot2 = subManager.getActiveSubscriptionInfo(1).getMnc();
}
Question https://stackoverflow.com/a/32871156/4860513 mention the same Android Limitation. So, I really believe it is not possible to achieve that before API22 with pure Android SDK.
回答2:
SubscriptionManager subManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
== PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
if (subManager.getActiveSubscriptionInfoCount() >= 1) {
List<SubscriptionInfo> subscriptionInfoList;
subscriptionInfoList=subManager.getActiveSubscriptionInfoList();
mccSlot1 = subscriptionInfoList.get(0).getMcc();
mncSlot1 = subscriptionInfoList.get(0).getMnc();
Toast.makeText(this, "Sim slot 1 " + mccSlot1 + " " + mncSlot1, Toast.LENGTH_SHORT).show();
mccSlot2 = subscriptionInfoList.get(1).getMcc();
mncSlot2 = subscriptionInfoList.get(1).getMnc();
Toast.makeText(this, "Sim slot 1 " + mccSlot2 + " " + mncSlot2, Toast.LENGTH_SHORT).show();
}
} else {
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager == null) {
return;
}
String mCCMncCode = telephonyManager.getNetworkOperator();
String mccCode = null;
String mncCode = null;
if (TextUtils.isEmpty(mCCMncCode)) {
return;
}
final int MNC_CODE_LENGTH = 3;
if (mCCMncCode.length() >= MNC_CODE_LENGTH) {
mccCode = mCCMncCode.substring(0, MNC_CODE_LENGTH);
}
if (mCCMncCode.length() > MNC_CODE_LENGTH) {
mncCode = mCCMncCode.substring(MNC_CODE_LENGTH);
}
Toast.makeText(this, "Lower than API 22 " + mccCode + " " + mncCode, Toast.LENGTH_SHORT).show();
return;
}
}
else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
}
回答3:
SubscriptionManager subManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> subscriptionInfoList=new ArrayList<>();
subscriptionInfoList=subManager.getActiveSubscriptionInfoList();
Log.d("LIST LIST",subscriptionInfoList.toString());
The Log was
[{id=4, iccId=89918610400110260628 simSlotIndex=0 displayName=Jio 4G carrierName=Jio 4G nameSource=0 iconTint=-16746133 dataRoaming=0 iconBitmap=android.graphics.Bitmap@2758949 mcc 405 mnc 861 SimProvisioningStatus 0 status 1}, {id=1, iccId=8991000900229271344 simSlotIndex=1 displayName=airtel carrierName=airtel nameSource=2 iconTint=-16746133 dataRoaming=0 iconBitmap=android.graphics.Bitmap@355e24e mcc 404 mnc 45 SimProvisioningStatus 0 status 1}]
来源:https://stackoverflow.com/questions/40902128/how-to-get-mcc-and-mnc-below-lollipop-mr1-devices-in-android