判断网络类型及是否有网络
程序员文章站
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";
}
上一篇: 判断是否有网
下一篇: 当军婚遭遇爱情时该如何维护?