Checkin for internet connection availability in Android.

This will be the initial set up to handle any network related operations.

Add below permissions in your manifest file:


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


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


Use below lines of code to get the relevant information:


NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();


            if (info == null || !info.isConnected())
          {
               Log.d("msg","no internet connection");
            }
            else
            {
                Log.d("msg"," internet connection available...");
            }


Happy coding :-)

No comments:

Post a Comment