Android 获取网络类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2919414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:54:18  来源:igfitidea点击:

Get Network type

android

提问by Moshik

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_IDENor NETWORK_TYPE_UMTS.. and so on..

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

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..

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

回答by Mr_and_Mrs_D

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

回答by outkkast

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

回答by RoflcoptrException

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

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

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#getNetworkTypeor NetworkInfo#getSubtypeName

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

回答by diyism

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

回答by sonida

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

回答by Bamboo Chemist

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

提供的网络类型列表。

回答by Krishna Shetty

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

此外,在您的 AndroidManifest.xml 中添加以下所需的权限。

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

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

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

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

回答by Lou Morda

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.

根据我的经验......最好使用 Android 培训指南进行这些类型的工作。当你使用这些类时很容易得到空指针异常,当你在应用程序第一次打开然后应用程序崩溃时尝试检测这些连接时尤其糟糕。

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:

您可以使用 ConnectivityManager 来检查您是否确实已连接到 Internet,如果是,则连接的类型是什么:

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

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

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

您可以使用 ConnectivityManager 来确定活动的无线电:

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

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

回答by Rick

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()

回答by Milad shoeibi

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