我想知道两张SIM卡的运营商名称。我这样做但是它在5.0.2设备中不起作用。
这是我的代码:
operator1 = telephonyInfo.getOutput(getApplicationContext(),
"getNetworkOperatorName", 0);
operator2 = telephonyInfo.getOutput(getApplicationContext(),
"getNetworkOperatorName", 1);
TelePhonyInfo:
public final class TelephonyInfo {
private static TelephonyInfo telephonyInfo;
String optName1, optName2, imsiSIM1, imsiSIM2;
private boolean isSIM1Ready;
private boolean isSIM2Ready;
private boolean isSIM1Activated;
private boolean isSIM2Activated;
public boolean isSIM1Activated() {
return isSIM1Activated;
}
public void setSIM1Activated(boolean isSIM1Activated) {
this.isSIM1Activated = isSIM1Activated;
}
public boolean isSIM2Activated() {
return isSIM2Activated;
}
public void setSIM2Activated(boolean isSIM2Activated) {
this.isSIM2Activated = isSIM2Activated;
}
public String getImsiSIM1() {
return imsiSIM1;
}
public String getImsiSIM2() {
return imsiSIM2;
}
public String getoptName1() {
return imsiSIM2;
}
public String getoptName2() {
return imsiSIM2;
}
public boolean isSIM1Ready() {
return isSIM1Ready;
}
public boolean isSIM2Ready() {
return isSIM2Ready;
}
public boolean isDualSIM() {
return imsiSIM2 != null;
}
private TelephonyInfo() {
}
@SuppressWarnings("static-access")
public static TelephonyInfo getInstance(Context context) {
if (telephonyInfo == null) {
telephonyInfo = new TelephonyInfo();
TelephonyManager telephonyManager = ((TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE));
telephonyInfo.imsiSIM1 = telephonyManager.getDeviceId();
telephonyInfo.imsiSIM2 = null;
try {
telephonyInfo.imsiSIM1 = getDeviceIdBySlot(context,
"getDeviceIdGemini", 0);
telephonyInfo.imsiSIM2 = getDeviceIdBySlot(context,
"getDeviceIdGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
telephonyInfo.imsiSIM1 = getDeviceIdBySlot(context,
"getDeviceId", 0);
telephonyInfo.imsiSIM2 = getDeviceIdBySlot(context,
"getDeviceId", 1);
} catch (GeminiMethodNotFoundException e1) {
// Call here for next manufacturer's predicted method name
// if you wish
e1.printStackTrace();
}
}
telephonyInfo.optName1 = telephonyManager.getSimOperatorName();
telephonyInfo.optName2 = null;
telephonyInfo.optName1 = getOutput(context,
"getNetworkOperatorNameGemini", 0);
telephonyInfo.optName2 = getOutput(context,
"getNetworkOperatorNameGemini", 1);
Log.d("tagKrishna1234", "not go in this condition");
telephonyInfo.optName1 = getOutput(context,
"getNetworkOperatorName", 0);
telephonyInfo.optName2 = getOutput(context,
"getNetworkOperatorName", 1);
if (Build.VERSION.SDK_INT == 21) {
Log.d("tagKrishna1234", "go in this condition");
telephonyInfo.optName1 = telephonyInfo.getOutput(context,
"getNetworkOperator", 0);
telephonyInfo.optName1 = telephonyInfo.getOutput(context,
"getNetworkOperator", 1);
}
if (Build.VERSION.SDK_INT >= 21) {
telephonyInfo.optName1 = telephonyInfo.getOutput(context,
"getNetworkOperatorName", 1);
telephonyInfo.optName2 = telephonyInfo.getOutput(context,
"getNetworkOperatorName", 2);
}
telephonyInfo.isSIM1Activated = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_ABSENT;
telephonyInfo.isSIM2Activated = false;
try {
telephonyInfo.isSIM1Activated = getSIMStateBySlot(context,
"getSimStateGemini", 0);
telephonyInfo.isSIM2Activated = getSIMStateBySlot(context,
"getSimStateGemini", 1);
} catch (GeminiMethodNotFoundException e2) {
e2.printStackTrace();
try {
telephonyInfo.isSIM1Activated = getSIMStateBySlot(context,
"getSimState", 0);
telephonyInfo.isSIM2Activated = getSIMStateBySlot(context,
"getSimState", 1);
} catch (GeminiMethodNotFoundException e1) {
// Call here for next manufacturer's predicted method name
// if you wish
e1.printStackTrace();
}
}
telephonyInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;
telephonyInfo.isSIM2Ready = false;
try {
telephonyInfo.isSIM1Ready = getSIMStateBySlot(context,
"getSimStateGemini", 0);
telephonyInfo.isSIM2Ready = getSIMStateBySlot(context,
"getSimStateGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
telephonyInfo.isSIM1Ready = getSIMStateBySlot(context,
"getSimState", 0);
telephonyInfo.isSIM2Ready = getSIMStateBySlot(context,
"getSimState", 1);
} catch (GeminiMethodNotFoundException e1) {
// Call here for next manufacturer's predicted method name
// if you wish
e1.printStackTrace();
}
telephonyInfo.isSIM1Activated = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_ABSENT;
telephonyInfo.isSIM2Activated = false;
try {
telephonyInfo.isSIM1Activated = getSIMStateBySlot(context,
"getSimStateGemini", 0);
telephonyInfo.isSIM2Activated = getSIMStateBySlot(context,
"getSimStateGemini", 1);
} catch (GeminiMethodNotFoundException e1) {
e.printStackTrace();
try {
telephonyInfo.isSIM1Activated = getSIMStateBySlot(
context, "getSimState", 0);
telephonyInfo.isSIM2Activated = getSIMStateBySlot(
context, "getSimState", 1);
} catch (GeminiMethodNotFoundException e2) {
// Call here for next manufacturer's predicted method
// name
// if you wish
e1.printStackTrace();
}
}
}
}
return telephonyInfo;
}
public static String getOutput(Context context, String methodName,
int slotId) {
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> telephonyClass;
String reflectionMethod = null;
String output = null;
try {
telephonyClass = Class.forName(telephony.getClass().getName());
for (Method method : telephonyClass.getMethods()) {
String name = method.getName();
if (name.contains(methodName)) {
Class<?>[] params = method.getParameterTypes();
if (params.length == 1 && params[0].getName().equals("int")) {
reflectionMethod = name;
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (reflectionMethod != null) {
try {
output = getOpByReflection(telephony, reflectionMethod, slotId,
false);
} catch (Exception e) {
e.printStackTrace();
}
}
return output;
}
private static String getOpByReflection(TelephonyManager telephony,
String predictedMethodName, int slotID, boolean isPrivate) {
// Log.i("Reflection", "Method: " + predictedMethodName+" "+slotID);
String result = null;
try {
Class<?> telephonyClass = Class.forName(telephony.getClass()
.getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimID;
if (slotID != -1) {
if (isPrivate) {
getSimID = telephonyClass.getDeclaredMethod(
predictedMethodName, parameter);
} else {
getSimID = telephonyClass.getMethod(predictedMethodName,
parameter);
}
} else {
if (isPrivate) {
getSimID = telephonyClass
.getDeclaredMethod(predictedMethodName);
} else {
getSimID = telephonyClass.getMethod(predictedMethodName);
}
}
Object ob_phone;
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
if (getSimID != null) {
if (slotID != -1) {
ob_phone = getSimID.invoke(telephony, obParameter);
} else {
ob_phone = getSimID.invoke(telephony);
}
if (ob_phone != null) {
result = ob_phone.toString();
}
}
} catch (Exception e) {
e.printStackTrace();
// Log.i("Reflection", "Result: " + e.printStackTrace());
return null;
}
return result;
}
public static String getDeviceIdBySlot(Context context,
String predictedMethodName, int slotID)
throws GeminiMethodNotFoundException {
String imsi = null;
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class<?> telephonyClass = Class.forName(telephony.getClass()
.getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimID = telephonyClass.getMethod(predictedMethodName,
parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimID.invoke(telephony, obParameter);
if (ob_phone != null) {
imsi = ob_phone.toString();
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return imsi;
}
public static String getNetworkOperator(Context context,
String predictedMethodName, int slotID)
throws GeminiMethodNotFoundException {
String Operator = null;
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class<?> telephonyClass = Class.forName(telephony.getClass()
.getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSIMOperator = telephonyClass.getMethod(
predictedMethodName, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSIMOperator.invoke(telephony, obParameter);
if (ob_phone != null) {
Operator = ob_phone.toString();
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return Operator;
}
private static boolean getSIMStateBySlot(Context context,
String predictedMethodName, int slotID)
throws GeminiMethodNotFoundException {
boolean isReady = false;
boolean isActivated = false;
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class<?> telephonyClass = Class.forName(telephony.getClass()
.getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimStateGemini = telephonyClass.getMethod(
predictedMethodName, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimStateGemini.invoke(telephony, obParameter);
if (ob_phone != null) {
int simState = Integer.parseInt(ob_phone.toString());
if (simState == TelephonyManager.SIM_STATE_READY) {
isReady = true;
} else if (simState == TelephonyManager.SIM_STATE_ABSENT) {
isActivated = false;
}
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return isReady;
}
private static class GeminiMethodNotFoundException extends Exception {
private static final long serialVersionUID = -996812356902545308L;
public GeminiMethodNotFoundException(String info) {
super(info);
}
}
}
运算符名称在5.0.2中返回null在其他API中,它返回true运算符名称。 感谢。