开发者

Get Network type

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

when i say network type: i refer to know 开发者_JS百科this info: if the type is: NETWORK_TYPE_IDEN or NETWORK_TYPE_UMTS.. and so on..

i tried to use:

NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();

or

NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo 
            (ConnectivityManager.TYPE_MOBILE); 

but no success..

i am doing this coz i wanna know if the current network is IDEN, or if the current network is connected through wifi..


I hate magic numbers :

/**
 * You need to add:
 * 
 * <pre>
 *     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 * </pre>
 * 
 * in your AndroidManifest.xml.
 */
private String networkType() {
    TelephonyManager teleMan = (TelephonyManager)
            getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = teleMan.getNetworkType();
    switch (networkType) {
        case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
        case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
        case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
        case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
        case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
        case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
        case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
        case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
        case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
        case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
        case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
        case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
        case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
        case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
        case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
        case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
    }
    throw new RuntimeException("New type of network");
}


This works for me to check the network type...

TelephonyManager teleMan =  
            (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();

switch (networkType)
{
case 7:
    textV1.setText("1xRTT");
    break;      
case 4:
    textV1.setText("CDMA");
    break;      
case 2:
    textV1.setText("EDGE");
    break;  
case 14:
    textV1.setText("eHRPD");
    break;      
case 5:
    textV1.setText("EVDO rev. 0");
    break;  
case 6:
    textV1.setText("EVDO rev. A");
    break;  
case 12:
    textV1.setText("EVDO rev. B");
    break;  
case 1:
    textV1.setText("GPRS");
    break;      
case 8:
    textV1.setText("HSDPA");
    break;      
case 10:
    textV1.setText("HSPA");
    break;          
case 15:
    textV1.setText("HSPA+");
    break;          
case 9:
    textV1.setText("HSUPA");
    break;          
case 11:
    textV1.setText("iDen");
    break;
case 13:
    textV1.setText("LTE");
    break;
case 3:
    textV1.setText("UMTS");
    break;          
case 0:
    textV1.setText("Unknown");
    break;
}


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


I am using this function:

public String get_network()
       {Activity act=(Activity)context;
        String network_type="UNKNOWN";//maybe usb reverse tethering
        NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (active_network!=null && active_network.isConnectedOrConnecting())
           {if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
               {network_type="WIFI";
               }
            else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
                 {network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
                 }
           }
        return network_type;
       }


Best answer

public static String getNetworkType(Context context) {

            TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            int networkType = teleMan.getNetworkType();
            switch ( networkType ) {
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                    return "1xRTT";
                case TelephonyManager.NETWORK_TYPE_CDMA:
                    return "CDMA";
                case TelephonyManager.NETWORK_TYPE_EDGE:
                    return "EDGE";
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                    return "eHRPD";
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    return "EVDO rev. 0";
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    return "EVDO rev. A";
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                    return "EVDO rev. B";
                case TelephonyManager.NETWORK_TYPE_GPRS:
                    return "GPRS";
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                    return "HSDPA";
                case TelephonyManager.NETWORK_TYPE_HSPA:
                    return "HSPA";
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    return "HSPA+";
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                    return "HSUPA";
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    return "iDen";
                case TelephonyManager.NETWORK_TYPE_LTE:
                    return "LTE";
                case TelephonyManager.NETWORK_TYPE_UMTS:
                    return "UMTS";
                case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                    return "Unknown";
            }
            return "New type of network";
    }


/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;
/** Current network is GSM {@hide} */
public static final int NETWORK_TYPE_GSM = 16;
 /** Current network is TD_SCDMA {@hide} */
public static final int NETWORK_TYPE_TD_SCDMA = 17;
/** Current network is IWLAN {@hide} */
public static final int NETWORK_TYPE_IWLAN = 18;

List of networkType Provided.


Also, add below required permission in your AndroidManifest.xml.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.


In the Android SDK in TelephonyManager there is a hidden method that has to do with the mobile data classes, it can be useful to follow the changes of this method through the API levels to keep up to date in the definition of android for them

Note: The source code shown is from API 29 of the Android SDK

Get Network type


In Android API LEVEL 30 getNetworkType has deprecated. Use getDataNetworkType() , and provide android.permission.READ_PHONE_STATE


In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.

You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:

http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

You can use the ConnectivityManager to determine the active wireless radio:

http://developer.android.com/training/efficient-downloads/connectivity_patterns.html


Better use would be:

    NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    if (activeNetworkInfo == null) return false;
    switch (activeNetworkInfo.getType()) {
        case ConnectivityManager.TYPE_WIFI:
            return true;
    }
//        public static final int TYPE_BLUETOOTH = 7;
//        public static final int TYPE_DUMMY = 8;
//        public static final int TYPE_ETHERNET = 9;
//        public static final int TYPE_MOBILE = 0;
//        public static final int TYPE_MOBILE_DUN = 4;
//        /** @deprecated */
//        @Deprecated
//        public static final int TYPE_MOBILE_HIPRI = 5;
//        /** @deprecated */
//        @Deprecated
//        public static final int TYPE_MOBILE_MMS = 2;
//        /** @deprecated */
//        @Deprecated
//        public static final int TYPE_MOBILE_SUPL = 3;
//        public static final int TYPE_VPN = 17;
//        public static final int TYPE_WIFI = 1;
//        public static final int TYPE_WIMAX = 6;

Which other types you want to define and handle is up to you...

For those wanting a string identifier, better use:

activeNetworkInfo.getTypeName()
activeNetworkInfo.getSubtypeName()


//**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;
    }


If you want to know which type of network it is, you can use this :

private String getNetworkClass() {
    // network type
    TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
    switch (networkType) {
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            return "Unknown network";
        case TelephonyManager.NETWORK_TYPE_GSM:
            return " GSM";
        case TelephonyManager.NETWORK_TYPE_CDMA:
        case TelephonyManager.NETWORK_TYPE_1xRTT:
        case TelephonyManager.NETWORK_TYPE_IDEN:
            return " 2G";
        case TelephonyManager.NETWORK_TYPE_GPRS:
            return " GPRS (2.5G)";
        case TelephonyManager.NETWORK_TYPE_EDGE:
            return " EDGE (2.75G)";
        case TelephonyManager.NETWORK_TYPE_UMTS:
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
            return " 3G";
        case TelephonyManager.NETWORK_TYPE_HSPA:
        case TelephonyManager.NETWORK_TYPE_HSDPA:
        case TelephonyManager.NETWORK_TYPE_HSUPA:
            return " H (3G+)";
        case TelephonyManager.NETWORK_TYPE_EHRPD:
        case TelephonyManager.NETWORK_TYPE_HSPAP:
        case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
            return " H+ (3G++)";
        case TelephonyManager.NETWORK_TYPE_LTE:
        case TelephonyManager.NETWORK_TYPE_IWLAN:
            return " 4G";
        default:
            return " 4G+";
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜