如何检查何时网络在Android设备上不可用?
我使用android设备如何检查何时网络在Android设备上不可用?
我想在应用程序启动时检查当前的网络状态。
所以,我试试这个。
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERIVCE);
NetworkInfo ethernet = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected()) {
Log.d(TAG, "WIFI isConnected");
} else if (ethernet.isConnected()) {
Log.d(TAG, "Ethernet isConnected");
} else if (wifi.isAvaiable()) {
Log.d(TAG, "Wifi not connect, but wifi isAvailable");
} else {
Log.d(TAG, "not available network");
}
此源码检查Wi-Fi和以太网的网络状态。
但是当无法联网时。不检查。
我的设备如果使用wifi,请在我的设备上连接无线LAN。
当我的设备上断开无线局域网。我的来源是检查wifi.isAvailable()
总之,
如何检查时,网络不可用(电流源,当网络不可用检查wifi.isAvailable)
当连接无线局域网和Wifi关闭,请检查
wifi.isAvailable()
但是当断开无线局域网和Wifi的连接时,请检查wifi.isAvailable()
如何区分这种情况。
谢谢。
试试这个在线或离线检查设备。
private boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()){
return true;
}else return false;
}
你应该使用getActiveNetworkInfo()来检查当前活动的默认数据网络的连通性 - 无需检查特定的数据网络;无论是以太网还是Wi-Fi。
public static boolean NetAvailable(final Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
final boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
return isConnected;
}
试试这个功能
public boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
,并检查这样
if(isNetworkAvailable(this))
{
System.out.println(""Internet Connected);
}else
{
System.out.println(""Internet Not Connected);
}
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
对于了解网络工作状态使用这个片段
public static boolean isInternetAccess() {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://www.google.com")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Log.e(TAG, "Error checking internet connection", e);
}
return false;
}
如果此代码返回的失败结果意味着网络不可用,并且调用您的代码以获取可用网络。
您需要使用BroadcastReceiver进行连接检查。当状态改变时调用onReceive()方法。
public BroadcastReceiver internetConnectionReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
@SuppressWarnings("static-access")
NetworkInfo activeWIFIInfo = connectivityManager
.getNetworkInfo(connectivityManager.TYPE_WIFI);
if (activeWIFIInfo.isConnected() || activeNetInfo.isConnected()) {
}
}
};
注册接收机中的onResume()和在的onDestroy摧毁它()
@Override
protected void onResume() {
super.onResume();
registerReceiver(internetConnectionReciever, new IntentFilter(
"android.net.conn.CONNECTIVITY_CHANGE"));
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(internetConnectionReciever);
}
public static boolean checkInternetConnection(Context context) {
try {
ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
android.net.NetworkInfo ethernet = connec.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
if ((wifi != null && wifi.isConnected())
|| (mobile != null && mobile.isConnected())
|| (ethernet != null && ethernet.isConnected())) {
return true;
}
log("Connection", "Connection failed");
return false;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
使用此方法。你不需要检查wifi.isAvailable(),只需检查isConnected()
' wifi!= null'是否可以wifi列表检查? – chohyunwook
我不明白你的问题,但你想要可用的无线列表? –
检查http://*.com/questions/18741034/how-to-get-available-wifi-networks-and-display-them-in-a-list-in-android&http://*.com/问题/ 11898409/list-of-available-wifi-devices获取免费wifi列表 –
我认为这段代码将返回“主线程异常的android os网络” –
@MohammadHaidar你是对的,从工作线程调用这个 – Shrikant