当前位置: 首页>>代码示例>>Java>>正文


Java NetworkInfo.getType方法代码示例

本文整理汇总了Java中android.net.NetworkInfo.getType方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInfo.getType方法的具体用法?Java NetworkInfo.getType怎么用?Java NetworkInfo.getType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.net.NetworkInfo的用法示例。


在下文中一共展示了NetworkInfo.getType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getProxy

import android.net.NetworkInfo; //导入方法依赖的package包/类
public static NetworkProxy getProxy(Context context) {
    if (context == null) {
        return null;
    }
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
    if (connectivityManager == null) {
        return null;
    }
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetworkInfo == null) {
        return null;
    }
    if (activeNetworkInfo.getType() == 0) {
        Object b = b(context);
        int a = a(context);
        if (!TextUtils.isEmpty(b) && a >= 0) {
            return new NetworkProxy(b, a);
        }
    }
    return null;
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:22,代码来源:HttpUtils.java

示例2: onReceive

import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    Intent serviceIntent = new Intent(context, AutoLoginService.class);
    try {
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            Log.d(LOG_TAG, "Have Wifi Connection");
            AutoLoginService.setState(context,LoginState.START);
            context.startService(serviceIntent);
        }
        else {
            Log.d(LOG_TAG, "Don't have Wifi Connection");
            AutoLoginService.setState(context,LoginState.STOPPED);
            context.stopService(serviceIntent);
        }
    }
    catch (Exception ex){
        Log.e(LOG_TAG, ex.getMessage());
    }
}
 
开发者ID:debjeetmaj,项目名称:AndroidWifiAutoLogin,代码行数:22,代码来源:WifiSenseReceiver.java

示例3: a

import android.net.NetworkInfo; //导入方法依赖的package包/类
private boolean a() {
    if (this.d.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE", this.d.getPackageName()) != 0) {
        return false;
    }
    try {
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) this.d.getSystemService("connectivity")).getActiveNetworkInfo();
        if (!(activeNetworkInfo == null || activeNetworkInfo.getType() == 1)) {
            String extraInfo = activeNetworkInfo.getExtraInfo();
            if (extraInfo != null && (extraInfo.equals("cmwap") || extraInfo.equals("3gwap") || extraInfo.equals("uniwap"))) {
                return true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:18,代码来源:t.java

示例4: getNetworkType

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 获取当前网络类型
 *
 * @return -1:没有网络, 0:移动网络, 1:wifi网络
 */
public static int getNetworkType() {
    int status = TYPE_NONE;
    ConnectivityManager cm = (ConnectivityManager) BaseApplication.get().getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo == null) {
        return status;
    }
    int mType = netInfo.getType();
    if (mType == ConnectivityManager.TYPE_MOBILE) {
        status = TYPE_MOBILE;
    } else if (mType == ConnectivityManager.TYPE_WIFI) {
        status = TYPE_WIFI;
    }
    return status;
}
 
开发者ID:linsir6,项目名称:TripBuyer,代码行数:21,代码来源:NetworkUtil.java

示例5: hasNetworkConnection

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * Validate the network connection. Supports checking for WiFi only when
 * this settings has been set to true.
 *
 * @param activity Activity
 * @return Has network connection
 */
public static boolean hasNetworkConnection(Activity activity) {
    ConnectivityManager connMgr = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo == null)
        return false;

    if (PreferenceManager.getDefaultSharedPreferences(activity).getBoolean(Settings.PREF_WIFI_ONLY, false))
        return networkInfo.getType() == ConnectivityManager.TYPE_WIFI;

    return networkInfo.isConnected();
}
 
开发者ID:linklux,项目名称:android-schedules,代码行数:20,代码来源:NetUtil.java

示例6: isNetworkNotOnWifiType

import android.net.NetworkInfo; //导入方法依赖的package包/类
public static boolean isNetworkNotOnWifiType() {
    final ConnectivityManager manager = (ConnectivityManager) FileDownloadHelper.getAppContext().
            getSystemService(Context.CONNECTIVITY_SERVICE);

    if (manager == null) {
        FileDownloadLog.w(FileDownloadUtils.class, "failed to get connectivity manager!");
        return true;
    }

    final NetworkInfo info = manager.getActiveNetworkInfo();

    return info == null || info.getType() != ConnectivityManager.TYPE_WIFI;
}
 
开发者ID:yannanzheng,项目名称:FileDownloader-master,代码行数:14,代码来源:FileDownloadUtils.java

示例7: is3G

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * check is3G
 *
 * @return boolean
 */
public static boolean is3G(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null
            && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        return true;
    }
    return false;
}
 
开发者ID:jopenbox,项目名称:MvpRxJavaRetrofitOkhttp,代码行数:16,代码来源:NetworkUtil.java

示例8: isConnectedToWifi

import android.net.NetworkInfo; //导入方法依赖的package包/类
public static boolean isConnectedToWifi(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni != null && ni.isConnected()
                && ni.getType() == ConnectivityManager.TYPE_WIFI;
    }
 
开发者ID:gigabytedevelopers,项目名称:FireFiles,代码行数:9,代码来源:ConnectionUtils.java

示例9: bU

import android.net.NetworkInfo; //导入方法依赖的package包/类
public static boolean bU() {
    try {
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) f.getServiceContext().getSystemService("connectivity")).getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.getType() == 1;
    } catch (Exception e) {
        return false;
    }
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:9,代码来源:ar.java

示例10: isWifi

import android.net.NetworkInfo; //导入方法依赖的package包/类
public static boolean isWifi(Context context) {
    NetworkInfo networkINfo = ((ConnectivityManager) context.getSystemService("connectivity")
    ).getActiveNetworkInfo();
    if (networkINfo == null || networkINfo.getType() != 1) {
        return false;
    }
    return true;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:9,代码来源:Util.java

示例11: getDeviceNetworkIp

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 获取手机ip地址,如果WiFi可以,则返回WiFi的ip,否则就返回网络ip.
 *
 * @param context 上下文
 * @return ip
 */
public static String getDeviceNetworkIp(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context
            .CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {  //wifi
            WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifi.getConnectionInfo();
            int ip = wifiInfo.getIpAddress();
            return convertToIp(ip);
        } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { // gprs
            try {
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
                     en.hasMoreElements();) {
                    NetworkInterface intf = en.nextElement();
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                         enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        //这里4.0以上部分手机返回的地址将会是ipv6地址,而且在4G下也验证了返回的是
                        //ipv6,而服务器不处理ipv6地址
                        if (!inetAddress.isLoopbackAddress() && !(inetAddress
                                instanceof Inet6Address)) {
                            return inetAddress.getHostAddress().toString();
                        }

                    }

                }
            } catch (SocketException e) {
                Log.e(TAG, "e", e);
            }
        }
    }
    return DEFAULT_NETWORK_IP;
}
 
开发者ID:facetome,项目名称:smart_plan,代码行数:42,代码来源:Utilty.java

示例12: getCurrentNetworkType

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 获取当前网络的类型
 *
 * @param context 上下文
 * @return 当前网络的类型。具体类型可参照ConnectivityManager中的TYPE_BLUETOOTH、TYPE_MOBILE、TYPE_WIFI等字段。当前没有网络连接时返回NetworkUtils.NETWORK_TYPE_NO_CONNECTION
 */
public static int getCurrentNetworkType(Context context) {
    NetworkInfo networkInfo
            = ((ConnectivityManager) context.getSystemService(
            Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    return networkInfo != null
            ? networkInfo.getType()
            : NETWORK_TYPE_NO_CONNECTION;
}
 
开发者ID:liying2008,项目名称:Simpler,代码行数:15,代码来源:NetWorkUtils.java

示例13: getNetworkType

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 获取当前网络类型
 * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
 *
 * @param context 上下文
 * @return 网络类型
 * <ul>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_WIFI   } </li>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_4G     } </li>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_3G     } </li>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_2G     } </li>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_UNKNOWN} </li>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_NO     } </li>
 * </ul>
 */
public static NetworkType getNetworkType(Context context) {
    NetworkType netType = NetworkType.NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo(context);
    if (info != null && info.isAvailable()) {

        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = NetworkType.NETWORK_WIFI;
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {

                case NETWORK_TYPE_GSM:
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    netType = NetworkType.NETWORK_2G;
                    break;

                case NETWORK_TYPE_TD_SCDMA:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                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:
                    netType = NetworkType.NETWORK_3G;
                    break;

                case NETWORK_TYPE_IWLAN:
                case TelephonyManager.NETWORK_TYPE_LTE:
                    netType = NetworkType.NETWORK_4G;
                    break;
                default:
                    String subtypeName = info.getSubtypeName();
                    if ("TD-SCDMA".equalsIgnoreCase(subtypeName) ||
                            "WCDMA".equalsIgnoreCase(subtypeName) ||
                            "CDMA2000".equalsIgnoreCase(subtypeName)) {
                        netType = NetworkType.NETWORK_3G;
                    } else {
                        netType = NetworkType.NETWORK_UNKNOWN;
                    }
                    break;
            }
        } else {
            netType = NetworkType.NETWORK_UNKNOWN;
        }
    }
    return netType;
}
 
开发者ID:imliujun,项目名称:LJFramework,代码行数:69,代码来源:NetworkUtils.java

示例14: getNetworkType

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 获取当前网络类型
 * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
 *
 * @return 网络类型
 * <ul>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_WIFI   } </li>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_4G     } </li>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_3G     } </li>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_2G     } </li>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_UNKNOWN} </li>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_NO     } </li>
 * </ul>
 */
public static NetworkType getNetworkType() {
    NetworkType netType = NetworkType.NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo();
    if (info != null && info.isAvailable()) {

        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = NetworkType.NETWORK_WIFI;
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {

                case NETWORK_TYPE_GSM:
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    netType = NetworkType.NETWORK_2G;
                    break;

                case NETWORK_TYPE_TD_SCDMA:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                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:
                    netType = NetworkType.NETWORK_3G;
                    break;

                case NETWORK_TYPE_IWLAN:
                case TelephonyManager.NETWORK_TYPE_LTE:
                    netType = NetworkType.NETWORK_4G;
                    break;
                default:

                    String subtypeName = info.getSubtypeName();
                    if (subtypeName.equalsIgnoreCase("TD-SCDMA")
                            || subtypeName.equalsIgnoreCase("WCDMA")
                            || subtypeName.equalsIgnoreCase("CDMA2000")) {
                        netType = NetworkType.NETWORK_3G;
                    } else {
                        netType = NetworkType.NETWORK_UNKNOWN;
                    }
                    break;
            }
        } else {
            netType = NetworkType.NETWORK_UNKNOWN;
        }
    }
    return netType;
}
 
开发者ID:youth5201314,项目名称:XFrame,代码行数:69,代码来源:XNetworkUtils.java

示例15: checkNetworkType

import android.net.NetworkInfo; //导入方法依赖的package包/类
public static int checkNetworkType(Context context) {

        int netType = 0;
        //连接管理对象
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取NetworkInfo对象
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo == null)
            return netType;
        switch (networkInfo.getType()) {
            case ConnectivityManager.TYPE_WIFI:
            case ConnectivityManager.TYPE_WIMAX:
            case ConnectivityManager.TYPE_ETHERNET:
                return 1;

            case ConnectivityManager.TYPE_MOBILE:
                switch (networkInfo.getSubtype()) {
                    case TelephonyManager.NETWORK_TYPE_LTE:  // 4G
                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                        return 2;
                    case TelephonyManager.NETWORK_TYPE_UMTS: // 3G
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                        return 3;

                    case TelephonyManager.NETWORK_TYPE_GPRS: // 2G
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                        return 4;

                    default:
                        return netType;
                }

            default:

                return netType;
        }

    }
 
开发者ID:Justson,项目名称:AgentWebX5,代码行数:43,代码来源:AgentWebX5Utils.java


注:本文中的android.net.NetworkInfo.getType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。