获取双 SIM 安卓手机的 SIM 运营商名称。
我正在开发一个应用程序,我需要用户 SIM 卡和运营商名称的详细信息。我如何获得他的 SIM 卡号码和两个连接的运营商名称。
这是我的代码...
private 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();
return null;
}
//Log.i("Reflection", "Result: " + result);
return result;
}
调用方法是....
final String optName1 = getOutput(getApplicationContext(), "getSimOperatorName",0);
final String optName2 = getOutput(getApplicationContext(), "getSimOperatorName",1);