簡體   English   中英

獲取雙卡Android手機的SimOperatorName

[英]Get SimOperatorName for dual SIM android phone

獲取雙卡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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM