在项目中使用getContext方法的时候你有没有考虑过,在不同的场景下,取到的Context会有什么不同,在View,Fragment,Activity和Application中的getContext又究竟是怎么样?
下面来分析一下。
Context的中文翻译为:上下文; 语境; 环境; 背景,开发中经常称之为“上下文”,语文中“上下文”我们可以理解为语境,在程序中,可以理解为当前对象在程序中所处的一个环境,一个与系统交互的过程。比如一个Activity就是一个Context,一个Service也是一个Context。在Android中”上下文“被抽象为Context类,用户和操作系统的每一次交互都可以看做是一个场景,一个应用程序就可以看做有多个场景组合在一起的。
Context源码
/**
* 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.
*/
public abstract class Context {
/** @hide */
@IntDef(flag = true, prefix = { "MODE_" }, value = {
MODE_PRIVATE,
MODE_WORLD_READABLE,
MODE_WORLD_WRITEABLE,
MODE_APPEND,
})
@Retention(RetentionPolicy.SOURCE)
public @interface FileMode {}
/** @hide */
@IntDef(flag = true, prefix = { "MODE_" }, value = {
MODE_PRIVATE,
MODE_WORLD_READABLE,
MODE_WORLD_WRITEABLE,
MODE_MULTI_PROCESS,
})
@Retention(RetentionPolicy.SOURCE)
public @interface PreferencesMode {}
/** @hide */
@IntDef(flag = true, prefix = { "MODE_" }, value = {
MODE_PRIVATE,
MODE_WORLD_READABLE,
MODE_WORLD_WRITEABLE,
MODE_ENABLE_WRITE_AHEAD_LOGGING,
MODE_NO_LOCALIZED_COLLATORS,
})
@Retention(RetentionPolicy.SOURCE)
public @interface DatabaseMode {}
/**
* 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).
*/
public static final int MODE_PRIVATE = 0x0000;
/**
* File creation mode: allow all other applications to have read access to
* the created file.
* <p>
* As of {@link android.os.Build.VERSION_CODES#N} attempting to use this
* mode will throw a {@link SecurityException}.
*
* @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 {@link ContentProvider},
* {@link BroadcastReceiver}, and {@link 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.
* @see android.support.v4.content.FileProvider
* @see Intent#FLAG_GRANT_WRITE_URI_PERMISSION
*/
@Deprecated
public static final int MODE_WORLD_READABLE = 0x0001;
/**
* File creation mode: allow all other applications to have write access to
* the created file.
* <p>
* As of {@link android.os.Build.VERSION_CODES#N} attempting to use this
* mode will throw a {@link SecurityException}.
*
* @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 {@link ContentProvider},
* {@link BroadcastReceiver}, and {@link 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.
* @see android.support.v4.content.FileProvider
* @see Intent#FLAG_GRANT_WRITE_URI_PERMISSION
*/
@Deprecated
public static final int MODE_WORLD_WRITEABLE = 0x0002;
……
}