Get Network type

后端 未结 13 1095
后悔当初
后悔当初 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:09

    To get the network type (I think your talking about wifi or mobile) you can use this code snippet:

    ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    
    //mobile
    State mobile = conMan.getNetworkInfo(0).getState();
    
    //wifi
    State wifi = conMan.getNetworkInfo(1).getState();
    

    and then use it like that:

    if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
        //mobile
    } else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
        //wifi
    }
    

    To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName

提交回复
热议问题