The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


System Services

One of the problems that we have in Android app development is the overloading of terms. We have already seen how “layouts” sometimes refer to layout resources and sometimes refer to container classes like LinearLayout.

Another example comes in the name “service”. This was already used in a few places in Java (e.g., ExecutorService). Android then used it for one of our four app components. Android also uses “service” as part of the term “system service”… where system services have little to do with Java services or Android services.

What is a System Service?

System services are “manager”-type classes that you get by calling getSystemService() on some Context, such as an Activity or Service. Usually, system services are tied to lower-level device functionality, like telephony. However, not all low-level device functionality is exposed by means of system services; some have separate APIs implemented by other sorts of “manager” classes.

There are two flavors of getSystemService(). The one that you are likely to use is the one that takes a String parameter that is the name of the system service that you want. You get back a generic Object, which you then have to downcast to the specific type of system service that you are trying to use:

AlarmManager mgr=(AlarmManager)someContext.getSystemService(Context.ALARM_SERVICE);

API Level 23 finally added a type-safe version of getSystemService(). You pass in the Java class object for the system service and get an instance of that class back:

AlarmManager mgr=someContext.getSystemService(AlarmManager.class);

However, until your minSdkVersion rises to 23 or higher, you will not be able to use that version of getSystemService() on Context on older devices.

Alas, there is no backport of getSystemService() on ContextCompat from the Android Support library.

What System Services Are There?

There are many system services, with new ones coming every Android version release or two. Here are the major ones as of Android 6.0, with links to chapters that focus on them (where available):