• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Connectivity state in a service

scanetuk

Lurker
Hi,
I am trying to find a way to get the connectivity state in a service, found the code below but getSystemService is red underlined.
Thanks

public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
 
Got an android service running and first thing I would like it to do is check for a network connection, I found another snippet below but don't know how to pass a context to it??

private boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = cm.getActiveNetworkInfo();
if (network != null) {
return network.isAvailable();
}
return false;
}



if (isOnline(???))
{
//Code here
}

also this could be running long after the activity is destroyed, I am really not sure only started coding in Android 2 weeks ago.
Thanks for your help
 
Back
Top Bottom