欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断网络类型及是否有网络

程序员文章站 2022-05-19 18:56:23
...

判断当前是否有可用网络

public boolean checkNetworkAvailable() {
	boolean isNetworkAvailable = false;
	android.net.ConnectivityManager connManager = (android.net.ConnectivityManager)getApplicationContext().getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
	if(connManager.getActiveNetworkInfo() != null){
		isNetworkAvailable = connManager.getActiveNetworkInfo().isAvailable();
	}
	return isNetworkAvailable;
}

 

判断当前网络类型

public String getNetworkType()  {
	android.net.ConnectivityManager connManager = (android.net.ConnectivityManager)getApplicationContext().getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
	android.net.NetworkInfo.State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
	if(state == State.CONNECTED || state == State.CONNECTING){
		return "wifi";
	}

	//3G网络判断
	state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
	if(state == State.CONNECTED || state == State.CONNECTING){
		return "mobile";
	}
	return "none";

}