

This article aims to teach you what context.getSystemService method is and how to use it wisely

Purpose

Context.getSystemService method is used when we want to access one of few Android system-level services.

To name only few of managers that we can get:

LocationManager – access GPS device information

– access GPS device information NotificationManager – manage (create, cancel) notifications

– manage (create, cancel) notifications ConnectivityManager – access network connection details and manage those connections

– access network connection details and manage those connections LayoutInflater – inflate and create views from xml layout resource files

Full list of available managers could be found here

Every of those managers is a bridge to communicate and use system-level features from our application. Easiest way to illustrate those managers in connection with the whole android system, is to look a one of Google Android system architecture figures. As you can see there, system service – orange box lies in the middle and between application framework (software layer) and linux kernel (hardware layer)

Android system architecture

Source: https://source.android.com/devices/architecture

How to use

Context object is the only necessary thing to get any of those services. Here are few examples

class MyCustomApplication : Activity() { override fun onCreate() { super.onCreate() val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager // code using location manager } } 1 2 3 4 5 6 7 8 9 class MyCustomApplication : Activity ( ) { override fun onCreate ( ) { super . onCreate ( ) val locationManager = getSystemService ( Context . LOCATION_SERVICE ) as LocationManager // code using location manager } }

getSystemService usage in custom Application object

class MyCustomApplication : Application() { override fun onCreate() { super.onCreate() val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager // code using location manager } } 1 2 3 4 5 6 7 8 9 class MyCustomApplication : Application ( ) { override fun onCreate ( ) { super . onCreate ( ) val locationManager = getSystemService ( Context . LOCATION_SERVICE ) as LocationManager // code using location manager } }

getSystemService usage in custom Activity object

As you can see, to access one of those services we only need to call described method using Context object instance and cast result of getSystemService method.

It does not matter what type of context it its. It could be:

Activity ,

, Application ,

, Service

System services are cached per Context instance

Every service connection created and retrieved with getSystemService method on specific context object is cached/reused.

What does it mean? If you call getSystemService(Context.LOCATION_SERVICE) multiple times on same Activity object instance you will get same service object.

Let’s look at the example:

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val locationManager1 = getSystemService(Context.LOCATION_SERVICE) as LocationManager val locationManager2 = getSystemService(Context.LOCATION_SERVICE) as LocationManager val locationManager3 = getSystemService(Context.LOCATION_SERVICE) as LocationManager // code using location manager } } 1 2 3 4 5 6 7 8 9 10 11 12 class MainActivity : AppCompatActivity ( ) { override fun onCreate ( savedInstanceState : Bundle ? ) { super . onCreate ( savedInstanceState ) setContentView ( R . layout . activity_main ) val locationManager1 = getSystemService ( Context . LOCATION_SERVICE ) as LocationManager val locationManager2 = getSystemService ( Context . LOCATION_SERVICE ) as LocationManager val locationManager3 = getSystemService ( Context . LOCATION_SERVICE ) as LocationManager // code using location manager } }

Every of those locationManager1 , locationManager2 , locationManager3 actually refers to the same object in the memory.

But when you use different contexts objects different services objects will be created:

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val locationManagerFromActivity = getSystemService(Context.LOCATION_SERVICE) as LocationManager val locationManagerFromApplication = applicationContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager // locationManagerFromActivity.equals(locationManagerFromActivity) is false } } 1 2 3 4 5 6 7 8 9 10 11 12 class MainActivity : AppCompatActivity ( ) { override fun onCreate ( savedInstanceState : Bundle ? ) { super . onCreate ( savedInstanceState ) setContentView ( R . layout . activity_main ) val locationManagerFromActivity = getSystemService ( Context . LOCATION_SERVICE ) as LocationManager val locationManagerFromApplication = applicationContext . getSystemService ( Context . LOCATION_SERVICE ) as LocationManager // locationManagerFromActivity.equals(locationManagerFromActivity) is false } }

locationManagerFromActivity is not same object as locationManagerFromApplication so you have to be careful on that!

What to remember while using getSystemService method