7

I have made a network monitor app. Here I have successfully implemented all the things. I have the dual sim android phone. I know how to get the name of operator. But I want to that which sim is connected to internet? I have used this code, only to show user that the device is connected through mobile data. I want to be more specific that the device is currently using which operator's internet.

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";
    }
Tushar Monirul
  • 4,944
  • 9
  • 39
  • 49

3 Answers3

0

There are no API about multiple sims before API 22. You can contact your device manufacturer and check whether they have an SDK add-on to access multiple sims or not.

Since API 22, you can check for multiple SIMs using SubscriptionManager's method getActiveSubscriptionInfoList(). More details on Android Docs.

Please look multiple sims. Here are some discussion about multiple, Hope this can help you for finding a way to access multiple sim network.

Community
  • 1
  • 1
xxxzhi
  • 471
  • 3
  • 12
0

Check this code 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) {}
        }
    }
}

You can get all available methods by calling:

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Method[] methods = Class.forName(telephonyManager.getClass().getName()).getMethods();
krawa
  • 583
  • 6
  • 12
0

Yes you can do it by unofficially apis, below codes is worked :

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
}
zyc zyc
  • 3,805
  • 1
  • 14
  • 14