Abdullah Al Dokhi
Abdullah Al Dokhi

Reputation: 201

Android - how to pass context parameter to a method?

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

Answers (3)

Thomas Vos
Thomas Vos

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

Dmitry Gryazin
Dmitry Gryazin

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

Alexander
Alexander

Reputation: 48272

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

Upvotes: 0

Related Questions