0
  public boolean isNetworkConnectionAvailable(){
        ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null && activeNetwork.isConnected();
        if(isConnected) {

                Log.d("Network", "Connected");
                return true;

        }
        else{
            checkNetworkConnection();
            Log.d("Network","Not Connected");
            return false;
        }
    }

It shows cannot resolve connectivitymanager and Networkinfo....Please help.. Thanks in advance

Dexter
  • 1,421
  • 3
  • 22
  • 43
dinolin
  • 1
  • 2
  • Try rebuilding after cleaning project.. Try adding this directly import android.net.ConnectivityManager; import android.net.NetworkInfo; – Palak Darji Aug 26 '17 at 04:54
  • Check if your manifest has proper permissions, you should have "android.permission.ACCESS_NETWORK_STATE" permission. – Dexter Aug 26 '17 at 04:56

3 Answers3

2

Use this method work like charm...

private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager 
      = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();

}

You will also need:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Anand Diamond
  • 339
  • 1
  • 9
1

Try this util method. I created a util class and added this method to it

public class Utils {
         /**
         * ******************************************
         * Method to check whether the Internet is Connected
         * ******************************************
         */
        public static boolean IsNetworkConnected(Context context) {
            if (context != null) {
                ConnectivityManager connMgr = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                return (networkInfo != null && networkInfo.isConnected());
            } else {
                return false;
            }
        }
}

you can try this method as

boolean isConnected = Utils.IsNetworkConnected(mContext);

and before trying, add these permissionsto AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Arun Shankar
  • 2,603
  • 2
  • 26
  • 36
1

Give permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57