I want to add in my app boolean to check if device is connected to Internet or not. I found this question
But I can't get it to work in my app. This is the code:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
public static boolean hasActiveInternetConnection(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
}
} else {
}
return false;
}
It's in my MainActivity. I get this error:
The method isNetworkAvailable() in the type MainActivity is not applicable for the arguments (Context)
Why am I getting this error? What's wrong with my code? or I just need to put those methods in separate activities?