Home > Android > Check network connection

Check network connection


This is a small code to check network state on android phone.

It need for you to check network connection when your application need to connect to the internet

public boolean CheckNetworkConnection(Context context) {
ConnectivityManager conn = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conn.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
|| conn.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
return true;
else if (conn.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| conn.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED)
return false;
return false;
}

You need to set some permissions in Android Manifest file

– android.permission.INTERNET
– android.permission.ACCESS_NETWORK_STATE

Happy Coding!!!

Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: