Android Context Class in Depth

July 25th, 2012 Leave a comment
Like the article?
Android Context Class in Depth

Previously in other articles we have discussed the Android architecture and outlined how the operating system works. The Android OS system is complex and unusual and can take some time to understand. Some of the classes take on a critical role in the system and deserve a little deeper look. In this article we will look at one of the crucial classes for application designers: the Context Class.

Let’s start by looking deeper at the abstract class: android.context.Context. This class is rarely mentioned in manuals or articles but arguably, it is the most important class in the whole Android SDK. This is due to the fact that it has ancestors of classes like Activity, Application, BackupAgent and Service. Contextalso implements the biggest portion of functionality of those classes. This way, Context can provide access to various infrastructure functions provided by Android, which is important to know so you can take advantage of these in your applications.

Context class allows getting access to following managers:

AssetManager – Provides access to an application’s raw asset files. The object is retrieved through a special call to getAssets() method.

AccessibilityManager – A system level service that serves as an event dispatch for AccessibilityEvents. These events are generated when something notable happens in the user interface, for example an Activity starts, the focus or selection of a View changes etc…

AccountManager – Provides access to a centralized registry of the user’s online accounts.

ActivityManager – Provides methodsfor interacting with overall activities and services running in the system and getting certain system info like memory allocation.

AlarmManager – Provides access to the system alarm services. This allows you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

AudioManager – Handles management of volume, ringer modes and audio routing.

ClipboardManager – Handles access and modification of the contents for the global clipboard. This is an old text-only interface to the clipboard. See android.content.ClipboardManager for the modern API which is the new, recommended method of access.

ConnectivityManager – For management of network connections.

DevicePolicyManager – For working with global device policy management.

DownloadManager – For requesting HTTP downloads. This is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots.

DropBoxManager – For recording diagnostic logs.

InputMethodManager – For accessing input methods and the central system API to the overall input method framework (IMF) architecture, this arbitrates interaction between applications and the current input method.

NotificationManager – For controlling keyguard and for informing the user of background events and to notify the user of events that happen. This is how you tell the user that something has happened in the background. Notifications can take different forms: a persistent icon that goes in the status bar and is accessible through the launcher, (when the user selects it, a designated Intent can be launched), turning on or flashing LEDs on the device, or alerting the user by flashing the backlight, playing a sound, or vibrating.

LocationManager – For controlling location based updates. This class provides access to the system location services. These services allow applications to obtain periodic updates of the device’s geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

NfcManager – For using Near Field Communication (NFC) functionality.

PowerManager – For controlling power management, including “wake locks,” which let you keep the device on while you’re running long tasks.

SearchManager – For handling searches. In practice, you won’t interact with this class directly, as search services are provided through methods in Activity and the ACTION_SEARCH Intent.

SensorManager – For accessing sensors.

StorageManager – For accessing system storage functions. The storage manager handles Opaque Binary Blobs (OBBs). OBBs contain a filesystem that maybe be encrypted on disk and mounted on-demand from an application. OBBs are a good way of providing large amounts of binary assets without packaging them into APKs as they may be multiple gigabytes in size. However, due to their size, they’re most likely stored in a shared storage pool accessible from all programs. The system does not guarantee the security of the OBB file itself: if any program modifies the OBB, there is no guarantee that a read from that OBB will produce the expected output.

TelephonyManager – For handling management the telephony features of the device.

UiModeManager – For controlling UI modes.

WifiManager – For handling management of Wi-Fi access.

WindowManager – For accessing the system’s window manager.

As you can see, there is a lot of functionality that is taking place in the Context class. There are also other services that are not named “managers” that an instance of the Context class can use:

Vibrator – For interacting with the vibration hardware.

WallpaperService – For accessing wallpapers.

LayoutInflator – For inflating layout resources in this context. This class is used to instantiate layout XML file into its corresponding View objects.

As if all of the above wasn’t enough, there are also APIs and object associations that the Context object can provide access to:

  • API for working with database files (not DB functionality!)
  • API for working with files and directories
  • API for working with permissions
  • API for binding to services and starting/stopping them
  • API for working with cache
  • API for working with OBBs
  • API for getting strings and formatted text from package resources
  • API for getting various data relevant to the current package
  • API for working with the current Theme (see Resources.Theme)
  • Method for retrieving system services
  • API for handling broadcasts
  • API for starting and stopping activities
  • Method for starting Instrumentation object
  • ApplicationInfo object for the application
  • Context object associated with the current application process
  • ClassLoader object associated with the package
  • ContentResolver object associated with the package
  • Looper object associated with the main thread of the current process
  • Resources object associated with the package

When you are developing Android applications, keep in mind all of the things you can do with the Context class. If you are just getting started learning Android application development, it would be a step in the right direction to learn as much about the Context class as possible so you can have access to all of these different managers and services.

Help us spread the word!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
Don't miss another post! Receive updates via email!