Android之判断是否有网封装类

            Android之判断是否有网封装类

                 我们做项目的时候,一般都不能离开网络,下面是对判断是否有网类的封装
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.widget.Toast;

public class NetState {
	
	public final static int CONNECTION_NO      = 0; //无网络连接
	public static final int CONNECTION_HOME    = 1;//内网中wifi
	public final static int CONNECTION_OUTSIDE = 2;//外网中wifi或使用移动数据
	//检查网络状态
	public static int checkState(Context context){
		int state = CONNECTION_NO;
		ConnectivityManager connectivityManager= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(connectivityManager!=null){
			NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
			if(networkInfo!=null&&networkInfo.isAvailable()&&networkInfo.isConnected()){
				if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
					WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
					WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//					wifiInfo.getBSSID().equals(wifiMac);
					state = CONNECTION_HOME;
				}else {
					state =CONNECTION_OUTSIDE;
				}
			}
		} 
		return state;
	}
	
	
	/**
	 * 判断是否有网络
	 */
	@SuppressWarnings("deprecation")
	public static boolean IfNet(Context context){
		switch (NetState.checkState(context)) {
		case NetState.CONNECTION_NO:
			Toast.makeText(context, "网络断了哦,检查一下您的网络吧", Toast.LENGTH_SHORT).show();
			return true;
		default:
			break;
		}
		return false;
	}
	
	public static String getLocalMacAddress(Context context) {  
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
        WifiInfo info = wifi.getConnectionInfo();  
        return info.getMacAddress();  
    }  

    public static boolean isWifi(Context context) {      
        try {  
            WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
            return (wm!=null && WifiManager.WIFI_STATE_ENABLED == wm.getWifiState());  
        } catch (Exception e) {           
        }  
        return false;  
    }  
}
还有一些其它的可以参考下

public class NetworkDetector {
 
    public static boolean detect(Activity act) {
      
       ConnectivityManager manager = (ConnectivityManager) act
              .getApplicationContext().getSystemService(
                     Context.CONNECTIVITY_SERVICE);
      
       if (manager == null) {
           return false;
       }
      
       NetworkInfo networkinfo = manager.getActiveNetworkInfo();
      
       if (networkinfo == null || !networkinfo.isAvailable()) {
           return false;
       }
 
       return true;
    }
}
 
这个类只有一个静态方法,用来检测当前系统的网络是否可用。如果可用,返回true。
还要进一步说明的是,NetworkInfo类中有一个方法getType(),这个方法可以用来判断当前可用的网络是wifi,还是mobile等等。
 
再来看使用的例子。
 
view plaincopy to clipboardprint?
boolean networkState = NetworkDetector.detect(XXXActivity.this);   
if (!networkState) {   
    DialogUtil.openMsgDialog(XXXActivity.this,   
           android.R.drawable.ic_dialog_info,   
           “网络不可用,是否现在设置网络?”, android.R.string.ok,   
           android.R.string.cancel,   
           new DialogInterface.OnClickListener() {   
              @Override  
              public void onClick(DialogInterface dialog, int which) {   
                  startActivityForResult(new Intent(   
                         ACTION_WIRELESS_SETTINGS), 0);   
              }   
           }, new DialogInterface.OnClickListener() {   
              @Override  
              public void onClick(DialogInterface dialog, int which) {   
                  dialog.cancel();   
              }   
           }).show();   
}  
       boolean networkState = NetworkDetector.detect(XXXActivity.this);
       if (!networkState) {
           DialogUtil.openMsgDialog(XXXActivity.this,
                  android.R.drawable.ic_dialog_info,
                  “网络不可用,是否现在设置网络?”, android.R.string.ok,
                  android.R.string.cancel,
                  new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         startActivityForResult(new Intent(
                                ACTION_WIRELESS_SETTINGS), 0);
                     }
                  }, new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         dialog.cancel();
                     }
                  }).show();
       }



你可能感兴趣的:(android,是否有网)