简体   繁体   English

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

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

How would i know whether my device is connected the web or not? 我怎么知道我的设备是否连接到网络? How can i detect connectivity? 我如何检测连接? Any sample code? 任何示例代码?

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 NetworkInfo object. 从那里你需要获得一个NetworkInfo对象。 For most, this will mean using ConnectivityManager. 对于大多数人来说,这将意味着使用ConnectivityManager。 getActiveNetworkInfo() : 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();

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" />

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();

} 

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 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM