java - 获取双 SIM 卡安卓手机的 SimOperatorName

标签 java android telephonymanager dual-sim

获取双 SIM 卡 android 手机的 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);

最佳答案

private List<String> getNetworkOperator(final Context context) {
List<String> carrierNames = new ArrayList<>();
try {
    final String permission = Manifest.permission.READ_PHONE_STATE;
    if ( (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) && (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED) ){
        final List<SubscriptionInfo> subscriptionInfos = SubscriptionManager.from(context).getActiveSubscriptionInfoList();
        for (int i = 0; i < subscriptionInfos.size(); i++) {
            carrierNames.add(subscriptionInfos.get(i).getCarrierName().toString());
        }

    } else {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        // Get carrier name (Network Operator Name)
        carrierNames.add(telephonyManager.getNetworkOperatorName());

    }
} catch (Exception e) {
    e.printStackTrace();
}
return carrierNames;
}

关于java - 获取双 SIM 卡安卓手机的 SimOperatorName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43197642/

相关文章:

java - 如何知道我是否在 Android 上通话?

java - Java 中的二维 char 数组创建/初始化

java - 如何扩展 Java 以引入引用传递?

android - 微调器 - 弹出/ ListView 中的不同项目文本

Android - 如何检测拨出电话是否已接听或已接听?

Java数组的有效使用/替代

java序列化套接字输入流刷新

java - Apache HttpClient 4.0 无法在 Android 上为套接字超时

android - 具有 gradle 和 android 的不同 API 主机