I have this function which network connection
public boolean isNetworkConnected() {
ConnectivityManager conManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conManager.getActiveNetworkInfo();
if (netInfo == null) {
// There are no active networks.
return false;
} else {
return true;
}
}
But when i a trying to make it static so that i can use it in every activity it is throwing:
Cannot make a static reference to the non-static method getSystemService(String) from the type
I don't want to create the object of the class every time .
getSystemService
is a non static method of theContext
class, so in order to access it you need an object from the class Context.Typically you call it from inside an Activty wherethis
is also an object ofContext
. In order to fix you could pass a Context to your methodisNetworkConnected
now we can use static function
getContext()
to get the context which inherit fromCocos2dxActivity.java
Add the non-static dependencies as parameters: