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_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
0 Comment(s)