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


Java ConnectivityManager.getActiveNetworkInfo方法代码示例

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


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

示例1: isNetworkAvailable

import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
 * 判断网络是否可用
 */
public static boolean isNetworkAvailable(Context context) {

    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (null != connectivity) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (null != info && info.isConnected()) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}
 
开发者ID:lo625090140,项目名称:lqrwechatrongcloud,代码行数:19,代码来源:NetUtils.java

示例2: isOnLine

import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static boolean isOnLine(Context context) {
    boolean haveInternet = false;
    try {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mConnectivityManager != null && mNetworkInfo != null
                && mNetworkInfo.isConnectedOrConnecting()) {
            haveInternet = true;
        } /* end of if */
    } catch (Exception e) {
        haveInternet = false;
    } /* end of try-catch */

    return haveInternet;
}
 
开发者ID:Welloculus,项目名称:MobileAppForPatient,代码行数:17,代码来源:InternetUtils.java

示例3: isNetworkConnected

import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
 * 判断是否有网络连接
 *
 * @param context
 * @return
 */
public static boolean isNetworkConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            return mNetworkInfo.isAvailable();
        }
    }
    return false;
}
 
开发者ID:yy941002,项目名称:retrofit-OurRetrofit,代码行数:18,代码来源:HttpUtil.java

示例4: getNetState

import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
 * 返回当前网络状态
 *
 * @param context
 * @return
 */
public static int getNetState(Context context) {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo networkinfo = connectivity.getActiveNetworkInfo();
            if (networkinfo != null) {
                if (networkinfo.isAvailable() && networkinfo.isConnected()) {
                    if (!connectionNetwork()) {
                        return NET_CNNT_BAIDU_TIMEOUT;
                    } else {
                        return NET_CNNT_BAIDU_OK;
                    }
                } else {
                    return NET_NOT_PREPARE;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return NET_ERROR;
}
 
开发者ID:coding-dream,项目名称:TPlayer,代码行数:30,代码来源:NetworkUtil.java

示例5: isWiFi

import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
 * 网络类型
 *
 * @param context
 * @return true :是,false :否
 */
public static boolean isWiFi(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context

            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectMgr.getActiveNetworkInfo();
    if (info == null) {
        return false;
    } else {
        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        } else {
            return false;
        }
    }
}
 
开发者ID:wanliyang1990,项目名称:AppServiceRestFul,代码行数:22,代码来源:NetUtil.java

示例6: getActiveNetwork

import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static NetworkInfo getActiveNetwork(Context context){
    if (context == null)
        return null;
    ConnectivityManager mConnMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (mConnMgr == null)
        return null;
    NetworkInfo aActiveInfo = mConnMgr.getActiveNetworkInfo(); // 获取活动网络连接信息
    return aActiveInfo;
}
 
开发者ID:vidding,项目名称:cordova-plugins-auto-wifi,代码行数:11,代码来源:WifiToSG.java

示例7: upload

import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static Statistic upload(Context context, String str, Bundle bundle) throws MalformedURLException, IOException, NetworkUnavailableException, HttpStatusException {
    int size;
    int i;
    byte[] byteArray;
    if (context != null) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager != null) {
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo == null || !activeNetworkInfo.isAvailable()) {
                throw new NetworkUnavailableException(NetworkUnavailableException.ERROR_INFO);
            }
        }
    }
    Bundle bundle2 = new Bundle(bundle);
    String str2 = "";
    str2 = bundle2.getString("appid_for_getting_config");
    bundle2.remove("appid_for_getting_config");
    HttpClient httpClient = HttpUtils.getHttpClient(context, str2, str);
    HttpUriRequest httpPost = new HttpPost(str);
    Bundle bundle3 = new Bundle();
    for (String str22 : bundle2.keySet()) {
        Object obj = bundle2.get(str22);
        if (obj instanceof byte[]) {
            bundle3.putByteArray(str22, (byte[]) obj);
        }
    }
    httpPost.setHeader(HttpRequest.HEADER_CONTENT_TYPE, "multipart/form-data; boundary=3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f");
    httpPost.setHeader("Connection", "Keep-Alive");
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byteArrayOutputStream.write(getBytesUTF8("--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f\r\n"));
    byteArrayOutputStream.write(getBytesUTF8(encodePostBody(bundle2, "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f")));
    if (!bundle3.isEmpty()) {
        size = bundle3.size();
        byteArrayOutputStream.write(getBytesUTF8("\r\n--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f\r\n"));
        i = -1;
        for (String str222 : bundle3.keySet()) {
            i++;
            byteArrayOutputStream.write(getBytesUTF8("Content-Disposition: form-data; name=\"" + str222 + "\"; filename=\"" + "value.file" + "\"" + "\r\n"));
            byteArrayOutputStream.write(getBytesUTF8("Content-Type: application/octet-stream\r\n\r\n"));
            byteArray = bundle3.getByteArray(str222);
            if (byteArray != null) {
                byteArrayOutputStream.write(byteArray);
            }
            if (i < size - 1) {
                byteArrayOutputStream.write(getBytesUTF8("\r\n--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f\r\n"));
            }
        }
    }
    byteArrayOutputStream.write(getBytesUTF8("\r\n--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f--\r\n"));
    byteArray = byteArrayOutputStream.toByteArray();
    i = byteArray.length + 0;
    byteArrayOutputStream.close();
    httpPost.setEntity(new ByteArrayEntity(byteArray));
    HttpResponse execute = httpClient.execute(httpPost);
    size = execute.getStatusLine().getStatusCode();
    if (size == 200) {
        return new Statistic(a(execute), i);
    }
    throw new HttpStatusException(HttpStatusException.ERROR_INFO + size);
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:61,代码来源:Util.java

示例8: getNetworkTypeForLink

import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static int getNetworkTypeForLink(Context context) {
    try {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni != null) {
            if (ni.getType() == ConnectivityManager.TYPE_WIFI) {
                return LinkNetWorkType.WIFI;
            } else {
                if (ni.getType() == ConnectivityManager.TYPE_MOBILE) {
                    switch (ni.getSubtype()) {
                        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 LinkNetWorkType._2G;
                        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 12: // TelephonyManager.NETWORK_TYPE_EVDO_B:
                        case 14: // TelephonyManager.NETWORK_TYPE_EHRPD:
                        case 15: // TelephonyManager.NETWORK_TYPE_HSPAP:
                            return LinkNetWorkType._3G;
                        case 13: // TelephonyManager.NETWORK_TYPE_LTE:
                            return LinkNetWorkType._4G;
                        default:
                            return LinkNetWorkType._2G;
                    }
                }
            }
        }
    } catch (Exception e) {
        return LinkNetWorkType.UNKNOWN;
    }
    return LinkNetWorkType.UNKNOWN;
}
 
开发者ID:angcyo,项目名称:RLibrary,代码行数:40,代码来源:NetworkUtil.java

示例9: isNetworkConnected

import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
 * 判断是否有网络连接
 *
 * @param context
 * @return
 */
public static boolean isNetworkConnected(Context context) {
    if (context != null) {
        // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        // 获取NetworkInfo对象
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        //判断NetworkInfo对象是否为空
        if (networkInfo != null)
            return networkInfo.isAvailable();
    }
    return false;
}
 
开发者ID:funnyzhaov,项目名称:Tribe,代码行数:19,代码来源:NetWorkUtils.java

示例10: getNetworkTypeForLink

import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static int getNetworkTypeForLink(Context context) {
    try {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni != null) {
            if (ni.getType() == ConnectivityManager.TYPE_WIFI) {
                return LinkNetWorkType.WIFI;
            } else {
                if (ni.getType() == ConnectivityManager.TYPE_MOBILE) {
                    switch (ni.getSubtype()) {
                    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 LinkNetWorkType._2G;
                    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 12: // TelephonyManager.NETWORK_TYPE_EVDO_B:
                    case 14: // TelephonyManager.NETWORK_TYPE_EHRPD:
                    case 15: // TelephonyManager.NETWORK_TYPE_HSPAP:
                        return LinkNetWorkType._3G;
                    case 13: // TelephonyManager.NETWORK_TYPE_LTE:
                        return LinkNetWorkType._4G;
                    default:
                        return LinkNetWorkType._2G;
                    }
                }
            }
        }
    } catch (Exception e) {
        return LinkNetWorkType.UNKNOWN;
    }
    return LinkNetWorkType.UNKNOWN;
}
 
开发者ID:newDeepLearing,项目名称:decoy,代码行数:40,代码来源:NetworkUtil.java

示例11: checkNetwork

import android.net.ConnectivityManager; //导入方法依赖的package包/类
static boolean checkNetwork(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    }
    @SuppressLint("MissingPermission") NetworkInfo info = connectivity.getActiveNetworkInfo();
    return info != null && info.isConnected();
}
 
开发者ID:Justson,项目名称:AgentWeb,代码行数:9,代码来源:AgentWebUtils.java

示例12: isConnected

import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
 * 判断网络是否连接
 */
public static boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return false;
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo == null) {
        return false;
    }
    if (netInfo.isConnected()) {
        return true;
    }
    return false;
}
 
开发者ID:angcyo,项目名称:RLibrary,代码行数:17,代码来源:Network.java

示例13: isNetworkAvailableAndConnected

import android.net.ConnectivityManager; //导入方法依赖的package包/类
private boolean isNetworkAvailableAndConnected() {
    ConnectivityManager cm =
            (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

    boolean isNetworkAvailable = cm.getActiveNetworkInfo() != null;
    boolean isNetworkConnected = isNetworkAvailable &&
            cm.getActiveNetworkInfo().isConnected();

    return isNetworkConnected;
}
 
开发者ID:rsippl,项目名称:AndroidProgramming3e,代码行数:11,代码来源:PollService.java

示例14: getNetwork

import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static NetworkType getNetwork(Context context){

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

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

        if (isConnected == false)
            return NetworkType.None;

        int type = activeNetwork.getType();

        if (type == ConnectivityManager.TYPE_WIFI || type == ConnectivityManager.TYPE_ETHERNET)
            return NetworkType.Infrastructure;

        return NetworkType.Cellular;
    }
 
开发者ID:adriankeenan,项目名称:uob-timetable-android,代码行数:18,代码来源:AndroidUtilities.java

示例15: isNetworkEnable

import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static boolean isNetworkEnable(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    return info != null && info.isAvailable();
}
 
开发者ID:stdnull,项目名称:RunMap,代码行数:6,代码来源:SystemUtils.java


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