1. Download our Official Android App: Forums for Android!

What is getSystemService()?

Discussion in 'Android Development' started by Saathwik, Aug 31, 2017.

  1. Saathwik

    Saathwik Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    9
    Joined:
    Jul 14, 2017

    Jul 14, 2017
    9
    0
    15
    Male
    What does this function do?

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

    What does cm contain here? and what does CONNECTIVITY_SERVICE get?
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,417
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,417
    10,688
    1,988
    Male
    Software developer
    South West of England
    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.
     
  3. Saathwik

    Saathwik Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    9
    Joined:
    Jul 14, 2017

    Jul 14, 2017
    9
    0
    15
    Male
    I Know this but i want to know what getSystemService(CONNECTIVITY_SERVICE) does internally? What does CONNECTIVITY_SERVICE get?
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,417
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,417
    10,688
    1,988
    Male
    Software developer
    South West of England
    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.
     
  5. NBfan32

    NBfan32 Newbie
    Rank:
    None
    Points:
    26
    Posts:
    12
    Joined:
    Jul 21, 2017

    Jul 21, 2017
    12
    3
    26
    https://developer.android.com/reference/android/content/Context.html

    "CONNECTIVITY_SERVICE
    Use with getSystemService(Class) to retrieve a ConnectivityManager for handling management of network connections."

    getSystemService returns a ConnectivityManager-Object which is choosen by string argument as identifier. Would bet that CONNECTIVITY_SERVICE refers to an item in an enum, but not sure. Basically its an identifier.
     
  6. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,417
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,417
    10,688
    1,988
    Male
    Software developer
    South West of England
    Code (Text):
    1.  
    2. public static final String CONNECTIVITY_SERVICE = "connectivity";
    3.  
    getSystemService() is an abstract method on class Context, but I haven't tracked down which class provides an actual implementation of it.
     

Share This Page

Loading...