阳光沙滩
让学习编程变得简单
android开发如何判断当前的网络?是wifi还是4G呢?
发表于 2020-03-08    阅读次数 239

android开发如何判断当前的网络?是wifi还是4G呢?

我们在开发时,有时候某些场景需要判断网络情况。

比如说要下载文件时,提示用户是否在wifi连接情况下才下载。比如说在线看视频时,不是wifi模式我们要提示用户注意流量消耗。

判断网络连接状态

  • 添加权限
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

获取网络连接状态

 ConnectivityManager cm =
                (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
        Log.d(TAG,"isConnected -- > " + isConnected);
   

这样子就可以拿到网络连接状态了

如果为true表示已经连接,false表示没有连接,也就是断网了。

判断网络类型

boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

广播监听判断是2G,3G,4G网络类型

如果以 Android 7.0(API 级别 24)及更高版本为目标平台的应用在其清单中声明广播接收器,则它们不会收到 CONNECTIVITY_ACTION 广播。如果应用使用 Context.registerReceiver() 注册 BroadcastReceiver 且该上下文仍有效,则它们仍会收到 CONNECTIVITY_ACTION 广播。

所以还是通过动态注册来监听吧

注册广播,前面说了,不让静态注册,高版本。

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        receiver = new NetworkReceiver();
        this.registerReceiver(receiver, filter);

广播接受者


public class NetworkReceiver extends BroadcastReceiver {

    private static final String TAG = "NetworkReceiver";
    public static String strNetworkType = null;

    @Override
    public void onReceive(Context context,Intent intent) {
        ConnectivityManager conn = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = conn.getActiveNetworkInfo();
        if(networkInfo != null && networkInfo.isConnected()) {
            if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                strNetworkType = "WIFI";
                Log.d(TAG,"wifi...");
            } else if(networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                String _strSubTypeName = networkInfo.getSubtypeName();

                Log.d(TAG,"Network getSubtypeName : " + _strSubTypeName);

                // TD-SCDMA   networkType is 17
                int networkType = networkInfo.getSubtype();
                switch(networkType) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11
                        strNetworkType = "2G";
                        break;
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
                    case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12
                    case TelephonyManager.NETWORK_TYPE_HSPAP:  //api<13 : replace by 15
                        strNetworkType = "3G";
                        break;
                    case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13
                        strNetworkType = "4G";
                        break;
                    default:
                        // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
                        if(_strSubTypeName.equalsIgnoreCase("TD-SCDMA") ||
                                _strSubTypeName.equalsIgnoreCase("WCDMA") ||
                                _strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                            strNetworkType = "3G";
                        } else {
                            strNetworkType = _strSubTypeName;
                        }

                        break;
                }
                Log.d(TAG,"Network getSubtype : " + Integer.valueOf(networkType).toString());
            }
            Log.d(TAG,"网络类型 -- > " + strNetworkType);
        }
    }
}

以上网络类型判断来自民间博客

监听 结果

 D/NetworkReceiver: wifi...
 D/NetworkReceiver: 网络类型 -- > WIFI
 D/NetworkReceiver: wifi...
 D/NetworkReceiver: 网络类型 -- > WIFI
 D/NetworkReceiver: Network getSubtypeName : LTE
 D/NetworkReceiver: Network getSubtype : 13
 D/NetworkReceiver: 网络类型 -- > 4G

okay,安卓api常见,知道就好,不需要记。必要的时候,或者遇到问题了,上官网去看看就好。