Search code examples
androidandroid-networking

Check 2G network


I'm writing a broadcast receiver that listens to connectivity changes.

My goal is to check if the 2g network is available (wether there is wifi or not).

I've been reading several articles and rewriting my class but can't get to a proper result...

Any help would be great...


Solution

  • If you actually care about only being on 2G, this will work.

    private boolean is2gNetwork() {
        ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo network = conMgr.getActiveNetworkInfo();
    
        // Check we're connected with a mobile network
        if (null != network && network.isConnectedOrConnecting()
                && network.getType() == ConnectivityManager.TYPE_MOBILE) {
    
            // Check we're on GPRS or EDGE
            final int subType = network.getSubtype();
            return subType == TelephonyManager.NETWORK_TYPE_EDGE
                    || subType == TelephonyManager.NETWORK_TYPE_GPRS;
        }
    
        return false;
    }