如何检查网络连接上启用或禁用WiFi和3G(资料图)移动? [英] How to check network connection enable or disable in WIFI and 3G(data plan) in mobile?

查看:122
本文介绍了如何检查网络连接上启用或禁用WiFi和3G(资料图)移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,在我的应用程序,我想检查网络连接,比如我要检查在WiFi和3G网络连接(如印度人大多喜欢在移动数据计划),如何检查网络wifi和3G.naybody的人都知道,请给一些想法有关。

I am developing an android application,In my application,I want to check network connection,like i want to check network connection in wifi and 3G(like Indians mostly like data plan in mobile),how to check network in wifi and 3G.naybody knows,please give some idea about that.

感谢

推荐答案

下面是code段。它返回true,如果网络允许,否则为假

Here is code snippet. It returns true if network enable ,else false

private boolean netCheckin() {
    try {
        ConnectivityManager nInfo = (ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);
        nInfo.getActiveNetworkInfo().isConnectedOrConnecting();
        Log.d(tag, "Net avail:"
            + nInfo.getActiveNetworkInfo().isConnectedOrConnecting());
        ConnectivityManager cm = (ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            Log.d(tag, "Network available:true");
            return true;
        } else {
            Log.d(tag, "Network available:false");
            return false;
        }
    } catch (Exception e) {
        return false;
    }
}

这篇关于如何检查网络连接上启用或禁用WiFi和3G(资料图)移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆