Context源码解析

来源:互联网 发布:js回调函数原理 编辑:程序博客网 时间:2024/05/17 02:14

    • 抽象类
    • 文件创建模式-四种枚举类型
    • 服务绑定标志-三种枚举类型
    • 待续中

抽象类

源码:

/** * 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 {}

解析:
该类是一个抽象类,为访问应用程序的环境信息提供了全局的接口,其实现由安卓系统所提供。它允许访问特定应用的资源和类,同时为应用级别的操作所调用,例如启动活动、发送广播、以及接受意图等等。

文件创建模式-四种枚举类型

    /**     * 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).     * @see #MODE_WORLD_READABLE     * @see #MODE_WORLD_WRITEABLE     */    public static final int MODE_PRIVATE = 0x0000;    /**     * File creation mode: allow all other applications to have read access     * to the created file.     * @see #MODE_PRIVATE     * @see #MODE_WORLD_WRITEABLE     */    public static final int MODE_WORLD_READABLE = 0x0001;    /**     * File creation mode: allow all other applications to have write access     * to the created file.     * @see #MODE_PRIVATE     * @see #MODE_WORLD_READABLE     */    public static final int MODE_WORLD_WRITEABLE = 0x0002;    /**     * File creation mode: for use with {@link #openFileOutput}, if the file     * already exists then write data to the end of the existing file     * instead of erasing it.     * @see #openFileOutput     */    public static final int MODE_APPEND = 0x8000;

解析:
MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身或者与该应用共享同一个用户标志的程序访问,在该模式下,写入的内容会覆盖原文件的内容。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取。
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
MODE_APPEND:该模式会检查文件是否存在,存在就往文件追加内容而不是覆盖内容,否则就创建新文件并添加内容。

服务绑定标志-三种枚举类型

    /**     * Flag for {@link #bindService}: automatically create the service as long     * as the binding exists.  Note that while this will create the service,     * its {@link android.app.Service#onStartCommand}     * method will still only be called due to an     * explicit call to {@link #startService}.  Even without that, though,     * this still provides you with access to the service object while the     * service is created.     *     * <p>Specifying this flag also tells the system to treat the service     * as being as important as your own process -- that is, when deciding     * which process should be killed to free memory, the service will only     * be considered a candidate as long as the processes of any such bindings     * is also a candidate to be killed.  This is to avoid situations where     * the service is being continually created and killed due to low memory.     */    public static final int BIND_AUTO_CREATE = 0x0001;    /**     * Flag for {@link #bindService}: include debugging help for mismatched     * calls to unbind.  When this flag is set, the callstack of the following     * {@link #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.     */    public static final int BIND_DEBUG_UNBIND = 0x0002;    /**     * Flag for {@link #bindService}: don't allow this binding to raise     * the target service's process to the foreground scheduling priority.     * It will still be raised to the 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.     */    public static final int BIND_NOT_FOREGROUND = 0x0004;

解析:
BIND_AUTO_CREATE:
1、表示当收到绑定请求时,如果服务尚未创建,则即刻创建。要注意的是,虽然这将创建服务,但其onStartCommand方法仍然只会被startService显式调用。尽管这样,在服务被创建的时候,它仍然可以为您提供服务对象的访问。
2、指定这个标志也告诉了系统,要像对待自己的进程一样重要地对待这个服务。也就是说,当系统内存不足,需要销毁优先级组件来释放内存,即需要决定哪个进程应该被销毁来释放内存的时候,同时只有驻留该服务的进程成为被销毁对象的时候,服务才可被销毁。 这是防止出现由于低内存而导致服务不断地自生自灭的情况。
BIND_DEBUG_UNBIND:通常用于调试场景中判断绑定的服务是否正确,但其会引起内存泄漏,因此非调试目的不建议使用。
BIND_NOT_FOREGROUND:表示系统将阻止驻留该服务的进程具有前台优先级,仅在后台运行。

待续中……

0 0