Get Network type

后端 未结 13 1091
后悔当初
后悔当初 2020-12-01 08:35

I\'ve been trying to retrive the current network type, but no success

when i say network type: i refer to know this info: if the type is: NETWORK_TYPE_IDEN

13条回答
  •  悲哀的现实
    2020-12-01 09:22

    //**Return type of connection to network

    public static int getNetworkType(Context context) {
            if (!isNetworkConnected(context))
                return -1;
            ConnectivityManager conMan = (ConnectivityManager) context.
                    getSystemService(Context.CONNECTIVITY_SERVICE);
    
            //mobile
            State mobile = conMan.getNetworkInfo(0).getState();
            //wifi
            State wifi = conMan.getNetworkInfo(1).getState();
    
            int result = 0;
    
            if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
                result |= NETWORK_MOBILE;
            }
    
            if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
                result |= NETWORK_WIFI;
            }
    
            return result;
        }
    

提交回复
热议问题