Android 如何检查安卓设备是否连接到网络?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2789612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:24:34  来源:igfitidea点击:

How can i check whether an android device is connected to the web?

android

提问by Muhammad Maqsoodur Rehman

How would i know whether my device is connected the web or not? How can i detect connectivity? Any sample code?

我如何知道我的设备是否已连接到网络?如何检测连通性?任何示例代码?

回答by Dan Lew

First, you need permission to know whether the device is connected to the web or not. This needs to be in your manifest, in the <manifest>element:

首先,您需要获得许可才能知道设备是否已连接到网络。这需要在您的清单中,在<manifest>元素中:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Next, you need to get a reference to the ConnectivityManager:

接下来,您需要获取对ConnectivityManager的引用:

ConnectivityManager cm = (ConnectivityManager) Context.getSystemService(Context.CONNECTIVITY_SERVICE);

From there you need to obtain a NetworkInfoobject. For most, this will mean using ConnectivityManager. getActiveNetworkInfo():

从那里你需要获得一个NetworkInfo对象。对于大多数人来说,这意味着使用ConnectivityManager。getActiveNetworkInfo():

NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
    // There are no active networks.
    return false;
}

From there, you just need to use one of NetworkInfo's methods to determine if the device is connected to the internet:

从那里,您只需要使用 NetworkInfo 的方法之一来确定设备是否已连接到互联网:

boolean isConnected = ni.isConnected();

回答by Tim

First, you need permission to know whether the device is connected to the web or not. This needs to be in your manifest, in the element:

首先,您需要获得许可才能知道设备是否已连接到网络。这需要在您的清单中,在元素中:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

then

然后

ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if (connec != null && (
    (connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) || 
    (connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED))) { 

        //You are connected, do something online.

} else if (connec != null && (
    (connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.DISCONNECTED) ||
    (connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.DISCONNECTED ))) {            

        //Not connected.    
        Toast.makeText(getApplicationContext(), "You must be connected to the internet", Toast.LENGTH_LONG).show();

} 

回答by Himanshu Aggarwal

Add this permission in your AppManifest.xml file:

在您的 AppManifest.xml 文件中添加此权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

The method to check if network is available or not:

检查网络是否可用的方法:

boolean isNetworkAvailable() {
  ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

  NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
  boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
  return isConnected;
}

Source

来源