获取网络类型

时间:2010-05-27 08:05:22

标签: android

我一直在尝试检索当前的网络类型,但没有成功

当我说网络类型:我参考知道这个信息: 如果类型是:NETWORK_TYPE_IDENNETWORK_TYPE_UMTS ..等等..

我试图使用:

NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();

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

但没有成功..

我正在做这个因为我想知道当前网络是否是IDEN,或者当前网络是否通过wifi连接..

13 个答案:

答案 0 :(得分:16)

我讨厌神奇的数字:

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

答案 1 :(得分:15)

这对我来说可以检查网络类型......

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

答案 2 :(得分:14)

要获取网络类型(我认为您在讨论wifi或移动设备),您可以使用此代码段:

ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

//mobile
State mobile = conMan.getNetworkInfo(0).getState();

//wifi
State wifi = conMan.getNetworkInfo(1).getState();

然后像这样使用它:

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

要获取移动网络的类型,我会尝试TelephonyManager#getNetworkTypeNetworkInfo#getSubtypeName

答案 3 :(得分:7)

我正在使用此功能:

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

答案 4 :(得分:2)

最佳答案

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";
    }

答案 5 :(得分:2)

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

提供的networkType列表。

答案 6 :(得分:1)

另外,在AndroidManifest.xml中添加以下必需的权限。

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

否则,由于安全性异常,您将在获取ConnectivityManager句柄时遇到应用程序崩溃。

答案 7 :(得分:1)

在TelephonyManager中的Android SDK中,有一个与移动数据类有关的隐藏方法,通过API级别跟踪此方法的更改以使android的定义保持最新可能很有用。他们

注意:显示的源代码来自Android SDK的API 29

TelephonyManager.java in Android SDK API 29

答案 8 :(得分:0)

根据我的经验......最好使用Android培训指南来完成这些类型的工作。当您使用这些类时,很容易获得空指针异常,当您首次打开应用程序然后应用程序崩溃时尝试检测这些连接时,这种情况尤其糟糕。

您可以使用ConnectivityManager检查您是否确实已连接到互联网,如果是,则建立了什么类型的连接:

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

您可以使用ConnectivityManager确定有效的无线电:

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

答案 9 :(得分:0)

更好的用途是:

    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;

您想要定义和处理的其他类型取决于您......

对于那些想要字符串标识符的人,最好使用:

activeNetworkInfo.getTypeName()
activeNetworkInfo.getSubtypeName()

答案 10 :(得分:0)

// **返回到网络的连接类型

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

答案 11 :(得分:0)

如果您想知道它是哪种网络,可以使用:

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+";
    }
}

答案 12 :(得分:0)

在Android API LEVEL 30中,不推荐使用getNetworkType。使用getDataNetworkType(),并提供android.permission.READ_PHONE_STATE