Android determines whether the mobile phone is connected to the Internet

  • 2020-06-07 05:19:04
  • OfStack

This article describes the Android method to determine whether a mobile phone is connected to the Internet, to share for your reference. The specific steps are as follows:

First add the network-related permissions in AndroidManifest.xml:

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

The code to determine the networking is as follows:

ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);  
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 
if(networkInfo == null || !networkInfo.isAvailable()) 

    // A network is currently available  

else  

    // The network is currently unavailable  
}

I hope this article has been helpful for your Android programming.


Related articles: