Android Programming

Android Context

The Context is an abstract class that provides an interface to global information about an application environment.

General Uses of Context class

  1. To provide access to application-specific resources and classes.
  2. Allow upcalls to application level operations like:
    • launching activities.
    • broadcasting intents.
    • receiving intents.

Characteristics of Context class

First context belongs to android.content package:

package android.content;

And is an abstract class:

public abstract class Context..{}

Context derives directly from java.lang.Object.

public abstract class Context extends Object{}

Context's Child classes

This class has only two direct subclasses:

  1. ContextWrapper - an implementation of the Context that used for proxying, or delegating requests to another Context.
  2. MockContext - used to inject dependencies,mocks and monitors to classes you are testing.

However it has many subclasses(more than 50). Here are a few common ones:

  1. Application - A super class defined in the package. This class is used to maintain a global application state.
  2. Activity - A single focused thing that a user can do. Mostly represents a screen the user interacts with.
  3. Service - Is an android application component which denotes a desire to either perform long-running operations while not interacting with the user or give functionality to other applications to use.

Specialized Functionalities provided by Context

With Respect to Constants

These are functionalities with respect to dozens of public Constants provided by Context class to other classes. |No.|Functionality| |---|-------------| |1.|Context defines dozens of String constants to be used with various system services like ALARM_SERVICE,ACTIVITY_SERVICE,BATTERY_SERVICE,AUIDO_SERVICE,ACCOUNT_SERVICE,BLUETOOTH_SERVICE,CAMERA_SERVICE,DOWNLOAD_SERVICE,JOB_SECHEDULER_SERVICE,LOCATION_SERVICE,LAYOUT_INFLATER_SERVICE,INPUT_SERVICE etc. Thes are typically used with abstract getSystemService() of the same class call.| |2.|Context also defines several Integer constants used as flags for the bindService() call of the same class for several purposes.These include BIND_AUTO_CREATE,BIND_ABOVE_CLIENT,BIND_IMPORTANT etc.| |3.|It provides a few constants to be used for setting different modes during a few operations. For example: The MODE_APPEND constant is used to set File creation mode whereby if the file already exists then data is appended to it. The MODE_PRIVATE constant is used to set file creation mode however the created file can only be accessed by the calling application or those with similar ID.|

With Respect to Methods

These are just a few of the functionalities the Context class provides to other classes with respect to the public methods it does define:

No. Functionality Description
1. Checking various permissions. For example: the checkPermission(String permission, int pid, int uid) will determine whether a particular process and user ID running in the system has the passed permission,checkUriPermission(Uri uri, int pid, int uid, int modeFlags) will determine whether a particular process and user ID running in the system has permission to access the passed Uri etc.
2. Connect to or create application service. Context can be used to connect to application service or create it if needed.This done via the bindService(Intent service, ServiceConnection conn, int flags) method call.
3. Create other Contexts. For example createConfigurationContext(Configuration overrideConfiguration) will create a new Context object for the current context but with resources adjusted to the passed configuration,createPackageContext(String packageName, int flags) returns a new Context object for the given application name.
4. List Associated Databases and Files databaseList() will provide us with a string array with the private databases associated with this Context's application package while fileList() returns a string array with the private files associated with this Context's application package.
5. Delete Associated Database and File Context provides us methods: deleteDatabase(String name) to delete the SQLiteDatabase associated with this Context's application's package and deleteFile(String name) to delete the given private file associated with this Context's application package.
6. Getting Application Context. Context's getApplicationContext() will return us the single global Application object of the current process.
7. Getting Application Information. Via the getApplicationInfo() we can get the full application information of the current Context's package.