Class Context
- java.lang.Object
-
- android.content.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceContext.BindServiceFlags
-
Field Summary
Fields Modifier and Type Field Description static intBIND_ABOVE_CLIENTFlag for#bindService: indicates that the client application binding to this service considers the service to be more important than the app itself.static intBIND_ADJUST_WITH_ACTIVITYFlag 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 intBIND_ALLOW_OOM_MANAGEMENTFlag for#bindService: allow the process hosting the bound service to go through its normal memory management.static intBIND_AUTO_CREATEFlag for#bindService: automatically create the service as long as the binding exists.static intBIND_DEBUG_UNBINDFlag for#bindService: include debugging help for mismatched calls to unbind.static intBIND_FOREGROUND_SERVICEstatic intBIND_FOREGROUND_SERVICE_WHILE_AWAKEstatic intBIND_IMPORTANTFlag for#bindService: this service is very important to the client, so should be brought to the foreground process level when the client is.static intBIND_NOT_FOREGROUNDFlag for#bindService: don't allow this binding to raise the target service's process to the foreground scheduling priority.static intBIND_NOT_VISIBLEFlag for#bindService: Don't consider the bound service to be visible, even if the caller is visible.static intBIND_SHOWING_UIstatic intBIND_TREAT_LIKE_ACTIVITYstatic intBIND_VISIBLEstatic intBIND_WAIVE_PRIORITYFlag for#bindService: don't impact the scheduling or memory management priority of the target service's hosting process.static intMODE_APPENDFile creation mode: for use withopenFileOutput(java.lang.String, int), if the file already exists then write data to the end of the existing file instead of erasing it.static intMODE_ENABLE_WRITE_AHEAD_LOGGINGDatabase open flag: when set, the database is opened with write-ahead logging enabled by default.static intMODE_MULTI_PROCESSDeprecated.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 intMODE_PRIVATEFile 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 intMODE_WORLD_READABLEDeprecated.Creating world-readable files is very dangerous, and likely to cause security holes in applications.static intMODE_WORLD_WRITEABLEDeprecated.Creating world-writable files is very dangerous, and likely to cause security holes in applications.
-
Constructor Summary
Constructors Constructor Description Context()
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract AssetManagergetAssets()Return an AssetManager instance for your application's package.abstract FilegetDatabasePath(String name)Returns the absolute path on the filesystem where a database created with#openOrCreateDatabaseis stored.abstract FilegetFilesDir()Returns the absolute path to the directory on the filesystem where files created withopenFileOutput(java.lang.String, int)are stored.abstract PackageManagergetPackageManager()Return PackageManager instance to find global package information.abstract StringgetPackageName()Return the name of this application's package.abstract ResourcesgetResources()Return a Resources instance for your application's package.abstract FileOutputStreamopenFileOutput(String name, int mode)Open a private file associated with this Context's application package for writing.
-
-
-
Field Detail
-
MODE_PRIVATE
public static final 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).
-
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 asContentProvider,BroadcastReceiver, andandroid.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 asContentProvider,BroadcastReceiver, andandroid.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_APPEND
public static final int MODE_APPEND
File creation mode: for use withopenFileOutput(java.lang.String, int), if the file already exists then write data to the end of the existing file instead of erasing it.
-
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 asContentProvider.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, itsandroid.app.Service#onStartCommandmethod 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 flagBIND_ADJUST_WITH_ACTIVITY. For compatibility, old applications that don't specifyBIND_AUTO_CREATEwill automatically have the flagsBIND_WAIVE_PRIORITYandBIND_ADJUST_WITH_ACTIVITYset 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#unbindServicecall 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_VISIBLE
public static final int BIND_VISIBLE
- See Also:
- Constant Field Values
-
BIND_SHOWING_UI
public static final int BIND_SHOWING_UI
- 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
-
-
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.
-
openFileOutput
public abstract FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException
Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.No permissions are required to invoke this method, since it uses internal storage.
- Parameters:
name- The name of the file to open; can not contain path separators.mode- Operating mode. Use 0 orMODE_PRIVATEfor the default operation,MODE_APPENDto append to an existing file,MODE_WORLD_READABLEandMODE_WORLD_WRITEABLEto control permissions.- Returns:
- The resulting
FileOutputStream. - Throws:
FileNotFoundException- See Also:
MODE_APPEND,MODE_PRIVATE,MODE_WORLD_READABLE,MODE_WORLD_WRITEABLE,#openFileInput,#fileList,#deleteFile,FileOutputStream(String)
-
getFilesDir
public abstract File getFilesDir()
Returns the absolute path to the directory on the filesystem where files created withopenFileOutput(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#openOrCreateDatabaseis 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
-
-