Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to check if your device is connected to Internet

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 533
    Comment on it

    We need to check if device is connected to Internet on making every API call (or call to Remote server). The function below returns true if device is connected to Internet.

    //**This function tells whether the device having an Internet connection or not**///

        public static boolean isNetworkAvailable(Context context) 
        {
            ConnectivityManager connectivityManager 
            = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            //System.out.println(activeNetworkInfo.);
            return activeNetworkInfo != null;
        }
    

    We need to add the following permissions in the Manifest file

      <uses-permission android:name="android.permission.ACCESS&#95;NETWORK&#95;STATE" />   
      <uses-permission android:name="android.permission.INTERNET" />
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: