android判断网络是否连上

示例代码:

    import android.net.ConnectivityManager;

    final ConnectivityManager connMgr =

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

    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifi.isConnected() || mobile.isConnected())

        return true;
    else
        return false;



ConnectivityManager还有以下一些TYPE

intTYPE_BLUETOOTHThe Default Bluetooth data connection.
intTYPE_DUMMYDummy data connection.
intTYPE_ETHERNETThe Default Ethernet data connection.
intTYPE_MOBILEThe Default Mobile data connection.
intTYPE_MOBILE_DUNA DUN-specific Mobile data connection.
intTYPE_MOBILE_HIPRIA High Priority Mobile data connection.
intTYPE_MOBILE_MMSAn MMS-specific Mobile data connection.
intTYPE_MOBILE_SUPLA SUPL-specific Mobile data connection.
intTYPE_WIFIThe Default WIFI data connection.
intTYPE_WIMAXThe Default WiMAX data connection.


NetworkInfo还有以下方法

boolean isAvailable()
Indicates whether network connectivity is possible.
boolean isConnected()
Indicates whether network connectivity exists and it is possible to establish connections and pass data.
boolean isConnectedOrConnecting()
Indicates whether network connectivity exists or is in the process of being established.
boolean isFailover()
Indicates whether the current attempt to connect to the network resulted from the ConnectivityManager trying to fail over to this network following a disconnect from another network.
boolean isRoaming()
Indicates whether the device is currently roaming on this network.


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值