zoukankan      html  css  js  c++  java
  • 判断当前手机的网络类型

    需要用到上面的方法

    12.判断手机连接的网络类型(2G,3G,4G)

    联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO

    public class Constants {

        /**

         * Unknown network class

         */

        public static final int NETWORK_CLASS_UNKNOWN = 0;

        /**

         * wifi net work

         */

        public static final int NETWORK_WIFI = 1;

        /**

         * "2G" networks

         */

        public static final int NETWORK_CLASS_2_G = 2;

        /**

         * "3G" networks

         */

        public static final int NETWORK_CLASS_3_G = 3;

        /**

         * "4G" networks

         */

        public static final int NETWORK_CLASS_4_G = 4;

    }

    public static int getNetWorkClass(Context context) {

            TelephonyManager telephonyManager = (TelephonyManager) context

                    .getSystemService(Context.TELEPHONY_SERVICE);

            switch (telephonyManager.getNetworkType()) {

            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:

                return Constants.NETWORK_CLASS_2_G;

            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:

            case TelephonyManager.NETWORK_TYPE_EHRPD:

            case TelephonyManager.NETWORK_TYPE_HSPAP:

                return Constants.NETWORK_CLASS_3_G;

            case TelephonyManager.NETWORK_TYPE_LTE:

                return Constants.NETWORK_CLASS_4_G;

            default:

                return Constants.NETWORK_CLASS_UNKNOWN;

            }

        }

    13.判断当前手机的网络类型(WIFI还是2,3,4G)

    public static int getNetWorkStatus(Context context) {

            int netWorkType = Constants.NETWORK_CLASS_UNKNOWN;

            ConnectivityManager connectivityManager = (ConnectivityManager) context

                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isConnected()) {

                int type = networkInfo.getType();

                if (type == ConnectivityManager.TYPE_WIFI) {

                    netWorkType = Constants.NETWORK_WIFI;

                } else if (type == ConnectivityManager.TYPE_MOBILE) {

                    netWorkType = getNetWorkClass(context);

                }

            }

            return netWorkType;

        }

  • 相关阅读:
    第三篇:数据仓库系统的实现与使用(含OLAP重点讲解)
    Django框架ORM单表删除表记录_模型层
    Django创建模型_模型层
    Django框架ORM单表添加表记录_模型层
    Django框架打印orm转换过程中的sql_模型层
    Django框架创建数据库表时setting文件配置_模型层
    UCRT: VC 2015 Universal CRT, by Microsoft
    vs2015部署---下一代VC运行时库系统:the Universal CRT
    VS2015开发的C++应用如何不依赖Visual C++ 2015 redistributable?
    C++11并发之std::thread
  • 原文地址:https://www.cnblogs.com/Jingerxin/p/5327713.html
Copyright © 2011-2022 走看看