Reputation: 201
I'm trying to develop a simple app for my daughter but I'm not a professional :)
I was wondering how you can pass a context to a Boolean method?
My issue is, when trying to merge both codes below
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;}
with
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) {
Log.e(LOG_TAG, "Error checking internet connection", e);
}
} else {
Log.d(LOG_TAG, "No network available!");
}
return false;}
I am getting the error because I don't know how to pass a Context
parameter.
Upvotes: 2
Views: 8282
Reputation: 12581
You should replace this:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
With this:
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
Upvotes: 5
Reputation: 943
Yes, having a reference to a Context is often needed in Android.
Basically, if you have a look at the Context class doc, you'll see, that 2 very important classes are subclassed from it: Application and Activity (including all its variations like FragmentActivity, etc).
So the technique is straightforward: anywhere the Context is needed you do one of those
Pass a Activity/Application object as a Context
Use a Singleton pattern for keeping Context field (custom Application class is often used for it, see getApplicationContext())
Concerning your piece of code:
The getSystemService()
is actually a method of Context class, and I guess you call it somewhere inside of Activity object (as Activity is a Context subclass). So there is no reason to pass Context object in ... if (isNetworkAvailable(context)) ...
, just remove this argument until isNetworkAvailable()
method is kept in Activity.
Beware of keeping a strong reference to the context!
Upvotes: 2
Reputation: 48272
private boolean isNetworkAvailable(Context ctx) {
ConnectivityManager connectivityManager
= (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;}
Upvotes: 0