android判断网络

public class NetConnection  {
	static Context context;
	 Handler tickHandler;     
	public NetConnection(Context context){
		this.context=context;
    //  run();
	}
	public void run() {   
	       tickHandler = new Handler();   
	       tickHandler.post(tickRunnable);    
	   }   
  
	    private Runnable tickRunnable = new Runnable() {   
	        public void run() {   
	          //  postInvalidate(); 
	          checkNetworkInfo();
	            tickHandler.postDelayed(tickRunnable, 2000);   
	        }   
	    };   
	    
	    
	   public  static boolean checkNetworkInfo()    {  
	    	ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
	    	//mobile 3G Data Network      
	    	State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); 
	    	//txt3G.setText(mobile.toString());        //wifi     
	    	State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();    
	    	//txtWifi.setText(wifi.toString());            
	    	//如果3G网络和wifi网络都未连接,且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接    
	    	if(mobile==State.CONNECTED||mobile==State.CONNECTING)         
	    		return true;      
	    	if(wifi==State.CONNECTED||wifi==State.CONNECTING)     
	    		return true;
	    	else{
	    		LOG.print("没有网络");
	    		return false;
	    	}
	    	
	    	//进入手机中的wifi网络设置界面            }
	    	//进入无线网络配置界面     
	    	//context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
	    	
	    }
}

 

你可能感兴趣的:(android)