Class Context

  • Direct Known Subclasses:
    ContextImpl, ContextWrapper

    public abstract class Context
    extends Object
    Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BIND_ABOVE_CLIENT
      Flag for #bindService: indicates that the client application binding to this service considers the service to be more important than the app itself.
      static int BIND_ADJUST_WITH_ACTIVITY
      Flag for #bindService: If binding from an activity, allow the target service's process importance to be raised based on whether the activity is visible to the user, regardless whether another flag is used to reduce the amount that the client process's overall importance is used to impact it.
      static int BIND_ALLOW_OOM_MANAGEMENT
      Flag for #bindService: allow the process hosting the bound service to go through its normal memory management.
      static int BIND_AUTO_CREATE
      Flag for #bindService: automatically create the service as long as the binding exists.
      static int BIND_DEBUG_UNBIND
      Flag for #bindService: include debugging help for mismatched calls to unbind.
      static int BIND_FOREGROUND_SERVICE  
      static int BIND_FOREGROUND_SERVICE_WHILE_AWAKE  
      static int BIND_IMPORTANT
      Flag for #bindService: this service is very important to the client, so should be brought to the foreground process level when the client is.
      static int BIND_NOT_FOREGROUND
      Flag for #bindService: don't allow this binding to raise the target service's process to the foreground scheduling priority.
      static int BIND_NOT_VISIBLE
      Flag for #bindService: Don't consider the bound service to be visible, even if the caller is visible.
      static int BIND_SHOWING_UI  
      static int BIND_TREAT_LIKE_ACTIVITY  
      static int BIND_VISIBLE  
      static int BIND_WAIVE_PRIORITY
      Flag for #bindService: don't impact the scheduling or memory management priority of the target service's hosting process.
      static int MODE_APPEND
      File creation mode: for use with openFileOutput(java.lang.String, int), if the file already exists then write data to the end of the existing file instead of erasing it.
      static int MODE_ENABLE_WRITE_AHEAD_LOGGING
      Database open flag: when set, the database is opened with write-ahead logging enabled by default.
      static int MODE_MULTI_PROCESS
      Deprecated.
      MODE_MULTI_PROCESS does not work reliably in some versions of Android, and furthermore does not provide any mechanism for reconciling concurrent modifications across processes.
      static int MODE_PRIVATE
      File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).
      static int MODE_WORLD_READABLE
      Deprecated.
      Creating world-readable files is very dangerous, and likely to cause security holes in applications.
      static int MODE_WORLD_WRITEABLE
      Deprecated.
      Creating world-writable files is very dangerous, and likely to cause security holes in applications.
    • Constructor Summary

      Constructors 
      Constructor Description
      Context()  
    • Field Detail

      • MODE_WORLD_READABLE

        @Deprecated
        public static final int MODE_WORLD_READABLE
        Deprecated.
        Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as ContentProvider, BroadcastReceiver, and android.app.Service. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore. File creation mode: allow all other applications to have read access to the created file.
        See Also:
        MODE_PRIVATE, MODE_WORLD_WRITEABLE, Constant Field Values
      • MODE_WORLD_WRITEABLE

        @Deprecated
        public static final int MODE_WORLD_WRITEABLE
        Deprecated.
        Creating world-writable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as ContentProvider, BroadcastReceiver, and android.app.Service. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore. File creation mode: allow all other applications to have write access to the created file.
        See Also:
        MODE_PRIVATE, MODE_WORLD_READABLE, Constant Field Values
      • MODE_MULTI_PROCESS

        @Deprecated
        public static final int MODE_MULTI_PROCESS
        Deprecated.
        MODE_MULTI_PROCESS does not work reliably in some versions of Android, and furthermore does not provide any mechanism for reconciling concurrent modifications across processes. Applications should not attempt to use it. Instead, they should use an explicit cross-process data management approach such as ContentProvider.
        SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process. This behavior is sometimes desired in cases where the application has multiple processes, all writing to the same SharedPreferences file. Generally there are better forms of communication between processes, though.

        This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targetting such releases. For applications targetting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.

        See Also:
        #getSharedPreferences, Constant Field Values
      • MODE_ENABLE_WRITE_AHEAD_LOGGING

        public static final int MODE_ENABLE_WRITE_AHEAD_LOGGING
        Database open flag: when set, the database is opened with write-ahead logging enabled by default.
        See Also:
        #openOrCreateDatabase(String, int, CursorFactory), #openOrCreateDatabase(String, int, CursorFactory, DatabaseErrorHandler), SQLiteDatabase#enableWriteAheadLogging, Constant Field Values
      • BIND_AUTO_CREATE

        public static final int BIND_AUTO_CREATE
        Flag for #bindService: automatically create the service as long as the binding exists. Note that while this will create the service, its android.app.Service#onStartCommand method will still only be called due to an explicit call to #startService. Even without that, though, this still provides you with access to the service object while the service is created.

        Note that prior to android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH, not supplying this flag would also impact how important the system consider's the target service's process to be. When set, the only way for it to be raised was by binding from a service in which case it will only be important when that activity is in the foreground. Now to achieve this behavior you must explicitly supply the new flag BIND_ADJUST_WITH_ACTIVITY. For compatibility, old applications that don't specify BIND_AUTO_CREATE will automatically have the flags BIND_WAIVE_PRIORITY and BIND_ADJUST_WITH_ACTIVITY set for them in order to achieve the same result.

        See Also:
        Constant Field Values
      • BIND_DEBUG_UNBIND

        public static final int BIND_DEBUG_UNBIND
        Flag for #bindService: include debugging help for mismatched calls to unbind. When this flag is set, the callstack of the following #unbindService call is retained, to be printed if a later incorrect unbind call is made. Note that doing this requires retaining information about the binding that was made for the lifetime of the app, resulting in a leak -- this should only be used for debugging.
        See Also:
        Constant Field Values
      • BIND_NOT_FOREGROUND

        public static final int BIND_NOT_FOREGROUND
        Flag for #bindService: don't allow this binding to raise the target service's process to the foreground scheduling priority. It will still be raised to at least the same memory priority as the client (so that its process will not be killable in any situation where the client is not killable), but for CPU scheduling purposes it may be left in the background. This only has an impact in the situation where the binding client is a foreground process and the target service is in a background process.
        See Also:
        Constant Field Values
      • BIND_ABOVE_CLIENT

        public static final int BIND_ABOVE_CLIENT
        Flag for #bindService: indicates that the client application binding to this service considers the service to be more important than the app itself. When set, the platform will try to have the out of memory killer kill the app before it kills the service it is bound to, though this is not guaranteed to be the case.
        See Also:
        Constant Field Values
      • BIND_ALLOW_OOM_MANAGEMENT

        public static final int BIND_ALLOW_OOM_MANAGEMENT
        Flag for #bindService: allow the process hosting the bound service to go through its normal memory management. It will be treated more like a running service, allowing the system to (temporarily) expunge the process if low on memory or for some other whim it may have, and being more aggressive about making it a candidate to be killed (and restarted) if running for a long time.
        See Also:
        Constant Field Values
      • BIND_WAIVE_PRIORITY

        public static final int BIND_WAIVE_PRIORITY
        Flag for #bindService: don't impact the scheduling or memory management priority of the target service's hosting process. Allows the service's process to be managed on the background LRU list just like a regular application process in the background.
        See Also:
        Constant Field Values
      • BIND_IMPORTANT

        public static final int BIND_IMPORTANT
        Flag for #bindService: this service is very important to the client, so should be brought to the foreground process level when the client is. Normally a process can only be raised to the visibility level by a client, even if that client is in the foreground.
        See Also:
        Constant Field Values
      • BIND_ADJUST_WITH_ACTIVITY

        public static final int BIND_ADJUST_WITH_ACTIVITY
        Flag for #bindService: If binding from an activity, allow the target service's process importance to be raised based on whether the activity is visible to the user, regardless whether another flag is used to reduce the amount that the client process's overall importance is used to impact it.
        See Also:
        Constant Field Values
      • BIND_FOREGROUND_SERVICE_WHILE_AWAKE

        public static final int BIND_FOREGROUND_SERVICE_WHILE_AWAKE
        See Also:
        Constant Field Values
      • BIND_FOREGROUND_SERVICE

        public static final int BIND_FOREGROUND_SERVICE
        See Also:
        Constant Field Values
      • BIND_TREAT_LIKE_ACTIVITY

        public static final int BIND_TREAT_LIKE_ACTIVITY
        See Also:
        Constant Field Values
      • BIND_NOT_VISIBLE

        public static final int BIND_NOT_VISIBLE
        Flag for #bindService: Don't consider the bound service to be visible, even if the caller is visible.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Context

        public Context()
    • Method Detail

      • getAssets

        public abstract AssetManager getAssets()
        Return an AssetManager instance for your application's package.
      • getResources

        public abstract Resources getResources()
        Return a Resources instance for your application's package.
      • getPackageManager

        public abstract PackageManager getPackageManager()
        Return PackageManager instance to find global package information.
      • getPackageName

        public abstract String getPackageName()
        Return the name of this application's package.
      • getFilesDir

        public abstract File getFilesDir()
        Returns the absolute path to the directory on the filesystem where files created with openFileOutput(java.lang.String, int) are stored.

        No permissions are required to read or write to the returned path, since this path is internal storage.

        Returns:
        The path of the directory holding application files.
        See Also:
        openFileOutput(java.lang.String, int), #getFileStreamPath, #getDir
      • getDatabasePath

        public abstract File getDatabasePath​(String name)
        Returns the absolute path on the filesystem where a database created with #openOrCreateDatabase is stored.
        Parameters:
        name - The name of the database for which you would like to get its path.
        Returns:
        An absolute path to the given database.
        See Also:
        #openOrCreateDatabase