Android - how to pass context parameter to a method?

微笑、不失礼 提交于 2019-12-24 12:48:19

问题


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.


回答1:


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;
}



回答2:


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!




回答3:


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


来源:https://stackoverflow.com/questions/28075873/android-how-to-pass-context-parameter-to-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!