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

What is getSystemService()?

Saathwik

Lurker
What does this function do?

ConnectivityManager cm = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);

What does cm contain here? and what does CONNECTIVITY_SERVICE get?
 
CONNECTIVITY_SERVICE is a constant value, defined within the Context class

https://developer.android.com/reference/android/content/Context.html#CONNECTIVITY_SERVICE

getSystemService() is a method on the same class, which returns a reference to a system service object

https://developer.android.com/refer...Context.html#getSystemService(java.lang.Class<T>)

The returned parameter is of type Object, so you have to cast it to the appropriate class type - in this case ConnectivityManager.

So the key thing is that the value you pass in to this method must match up with the type you are casting to.
 
I Know this but i want to know what getSystemService(CONNECTIVITY_SERVICE) does internally? What does CONNECTIVITY_SERVICE get?
 
If you're interested in how that method works, then you can view the source code for it.
CONNECTIVITY_SERVICE is simply an integer value passed in to the method, which is used to lookup the correct service object. As I say, to understand exactly how it's doing this, you need to look at the source code.
 
Code:
public static final String CONNECTIVITY_SERVICE = "connectivity";

getSystemService() is an abstract method on class Context, but I haven't tracked down which class provides an actual implementation of it.
 
Back
Top Bottom