Skip to content

Instantly share code, notes, and snippets.

@rosuH
Created April 4, 2018 07:25
Show Gist options
  • Select an option

  • Save rosuH/ed51607d4d9126b80ef7ac690ba6c8ff to your computer and use it in GitHub Desktop.

Select an option

Save rosuH/ed51607d4d9126b80ef7ac690ba6c8ff to your computer and use it in GitHub Desktop.
Judge whether the network is available.
private boolean isNetworkAvailableAndConnected() {
ConnectivityManager cm = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
boolean isNetworkAvailable = cm.getActiveNetworkInfo() != null;
boolean isNetworkConnected = isNetworkAvailable && cm.getActiveNetworkInfo().isConnected();
return isNetworkConnected;
}
@rosuH

rosuH commented Apr 4, 2018

Copy link
Copy Markdown
Author

Add <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> in AndroidManifest.xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment