如何在双卡安卓手机中获取连接到互联网的运营商名称?

时间:2015-10-07 13:17:35

标签: android android-networking

我制作了一个网络监控应用程序。在这里,我成功地实现了所有的事情。我有双卡通android手机。我知道如何获得运营商的名称。但我想知道哪个SIM连接到互联网?我使用过此代码,仅向用户显示设备是通过移动数据连接的。我想更具体地说,该设备目前正在使用哪个运营商的互联网。

public static String isInternetConnected (Context ctx) {
        ConnectivityManager connectivityMgr = (ConnectivityManager) ctx
                .getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        // Check if wifi or mobile network is available or not. If any of them is
        // available or connected then it will return true, otherwise false;
        if (wifi != null) {
            if (wifi.isConnected()) {
                return "wifi";
            }
        }
        if (mobile != null) {
            if (mobile.isConnected()) {
                return "mobile";
            }
        }
        return "none";
    }

3 个答案:

答案 0 :(得分:0)

API 22之前没有关于多个SIM卡的API。您可以联系您的设备制造商,检查他们是否有SDK插件,以便访问多个SIM卡。

从API 22开始,您可以使用SubscriptionManager的方法getActiveSubscriptionInfoList()检查多个SIM。有关Android文档的更多详细信息。

请查看multiple sims。以下是关于多个的一些讨论,希望这可以帮助您找到访问多个SIM网络的方法。

答案 1 :(得分:0)

检查此代码https://github.com/dragos-niculescu/dualsim/blob/master/src/com/example/dualsim/TelephonyInfo.java

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int dataNetworkTypeSIM1 = telephonyManager.getNetworkType();
int dataNetworkTypeSIM2 = 0;
try {
    dataNetworkTypeSIM1 = Integer.parseInt(getStringOfInt(context, "getNetworkTypeGemini", 0));
    dataNetworkTypeSIM2 = Integer.parseInt(getStringOfInt(context, "getNetworkTypeGemini", 1));
} catch (GeminiMethodNotFoundException e) {
    try {
        dataNetworkTypeSIM1 = Integer.parseInt(getStringOfInt(context, "getDataNetworkTypeGemini", 0));
        dataNetworkTypeSIM2 = Integer.parseInt(getStringOfInt(context, "getDataNetworkTypeGemini", 1));
    } catch (GeminiMethodNotFoundException e1) {
        try {
             dataNetworkTypeSIM1 = Integer.parseInt(getStringOfInt(context, "getDataNetworkType", 0));
             dataNetworkTypeSIM2 = Integer.parseInt(getStringOfInt(context, "getDataNetworkType", 1));
        } catch (GeminiMethodNotFoundException e2) {
             try {
                  dataNetworkTypeSIM1 = Integer.parseInt(getStringOfInt(context, "getNetworkType", 0));
                  dataNetworkTypeSIM2 = Integer.parseInt(getStringOfInt(context, "getNetworkType", 1));
             } catch (GeminiMethodNotFoundException e3) {}
        }
    }
}

您可以致电:

获取所有可用的方法
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Method[] methods = Class.forName(telephonyManager.getClass().getName()).getMethods();

答案 2 :(得分:0)

是的,您可以通过非官方的api进行操作,以下代码可以正常工作:

fun getNetOperatorName(): String? {
    val manager = App.instance.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && manager.phoneCount > 1) {
        fun parseValues(raw: String?): List<String> {
            return raw?.split(",") ?: emptyList()
        }

        fun getSystemProperty(propName: String): String? {
            return try {
                Runtime.getRuntime().exec("getprop $propName").inputStream.use {
                    it.buffered(1024).bufferedReader().use { bf -> bf.readLine() }
                }
            } catch (ex: Exception) {
                Log.e("st-lite", "Unable to read sysprop $propName", ex)
                null
            }
        }

        try {
            val simOperatorCodes1 = getSystemProperty("gsm.operator.numeric")
            val simOperatorCodes2 = getSystemProperty("gsm.sim.operator.numeric")
            val codes1 = parseValues(simOperatorCodes1)
            val codes2 = parseValues(simOperatorCodes2)
            var simOperatorNames = getSystemProperty("gsm.operator.alpha")
            if (simOperatorNames.isNullOrEmpty()) {
                simOperatorNames = getSystemProperty("gsm.sim.operator.alpha")
            }
            val names = parseValues(simOperatorNames)
            val currSimOperator = manager.simOperator
            for (i in 0..codes1.size.coerceAtMost(codes2.size)) {
                if (codes1[i] == currSimOperator || codes2[i] == currSimOperator) {
                    return names[i]
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    return manager.networkOperatorName
}