當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。