android 链接网络成功,Android之网络连接判断是否成功

package com.example.util;

import android.content.Context;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.net.wifi.WifiManager;

import android.telephony.TelephonyManager;

import android.util.Log;

/**

*

* @author  XuZhiwei (xuzw13@gmail.com)

* Weibo:http://weibo.com/xzw1989

* Create at 2012-9-22 上午11:25:04

*/

public class NetUtil {

/**

* 判断Network是否开启(包括移动网络和wifi)

*

* @return

*/

public static boolean isNetworkEnabled(Context mContext) {

return ( isNetEnabled(mContext)|| isWIFIEnabled(mContext));

}

/**

* 判断Network是否连接成功(包括移动网络和wifi)

* @return

*/

public static boolean isNetworkConnected(Context mContext){

return (isWifiContected(mContext) || isNetContected(mContext));

}

/**

* 判断移动网络是否开启

*

* @return

*/

public static boolean isNetEnabled(Context context) {

boolean enable = false;

TelephonyManager telephonyManager = (TelephonyManager) context

.getSystemService(Context.TELEPHONY_SERVICE);

if (telephonyManager != null) {

if (telephonyManager.getNetworkType() != TelephonyManager.NETWORK_TYPE_UNKNOWN) {

enable = true;

Log.i(Thread.currentThread().getName(), "isNetEnabled");

}

}

return enable;

}

/**

* 判断wifi是否开启

*/

public static boolean isWIFIEnabled(Context context) {

boolean enable = false;

WifiManager wifiManager = (WifiManager) context

.getSystemService(Context.WIFI_SERVICE);

if (wifiManager.isWifiEnabled()) {

enable = true;

Log.i(Thread.currentThread().getName(), "isWifiEnabled");

}

Log.i(Thread.currentThread().getName(), "isWifiDisabled");

return enable;

}

/**

* 判断移动网络是否连接成功!

* @param context

* @return

*/

public static boolean isNetContected(Context context){

ConnectivityManager connectivityManager

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

NetworkInfo mobileNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if(mobileNetworkInfo.isConnected())

{

Log.i(Thread.currentThread().getName(), "isNetContected");

return true ;

}

Log.i(Thread.currentThread().getName(), "isNetDisconnected");

return false ;

}

/**

* 判断wifi是否连接成功

* @param context

* @return

*/

public static boolean isWifiContected(Context context){

ConnectivityManager connectivityManager

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

NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if(wifiNetworkInfo.isConnected())

{

Log.i(Thread.currentThread().getName(), "isWifiContected");

return true ;

}

Log.i(Thread.currentThread().getName(), "isWifiDisconnected");

return false ;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值