码迷,mamicode.com
首页 > 移动开发 > 详细

Application,Service,Broadcast类

时间:2017-10-25 19:41:26      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:重写   interface   single   cut   uil   类构造   change   clu   base   

Application这个类会在应用程序的所有组件初始化前被创建,保存程序的全局状态。子类名字通过“android:name”指定。

 1 30/**
 2 31 * Base class for maintaining global application state. You can provide your own
 3 32 * implementation by creating a subclass and specifying the fully-qualified name
 4 33 * of this subclass as the <code>"android:name"</code> attribute in your
 5 34 * AndroidManifest.xml‘s <code>&lt;application&gt;</code> tag. The Application
 6 35 * class, or your subclass of the Application class, is instantiated before any
 7 36 * other class when the process for your application/package is created.
 8 37 *
 9 38 * <p class="note"><strong>Note: </strong>There is normally no need to subclass
10 39 * Application.  In most situations, static singletons can provide the same
11 40 * functionality in a more modular way.  If your singleton needs a global
12 41 * context (for example to register broadcast receivers), include
13 42 * {@link android.content.Context#getApplicationContext() Context.getApplicationContext()}
14 43 * as a {@link android.content.Context} argument when invoking your singleton‘s
15 44 * <code>getInstance()</code> method.
16 45 * </p>
17 46 */

继承于ContextWrapper,实现了ComponentCallbacks2接口

1 47public class Application extends ContextWrapper implements ComponentCallbacks2 {

三个ArrayList,ComponentCallbacks,ActivityLifecycleCallbacks,OnProvideAssistDataListener

1 48    private ArrayList<ComponentCallbacks> mComponentCallbacks =
2 49            new ArrayList<ComponentCallbacks>();
3 50    private ArrayList<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
4 51            new ArrayList<ActivityLifecycleCallbacks>();
5 52    private ArrayList<OnProvideAssistDataListener> mAssistCallbacks = null;

有个hide的LoadedApk

1 54    /** @hide */
2 55    public LoadedApk mLoadedApk;
3 56

内部接口ActivityLifecycleCallbacks,有onActivity Created,Started,Resumed,Paused,Stopped,SaveInstanced,Destroyed这几个方法

1 57    public interface ActivityLifecycleCallbacks {
2 58        void onActivityCreated(Activity activity, Bundle savedInstanceState);
3 59        void onActivityStarted(Activity activity);
4 60        void onActivityResumed(Activity activity);
5 61        void onActivityPaused(Activity activity);
6 62        void onActivityStopped(Activity activity);
7 63        void onActivitySaveInstanceState(Activity activity, Bundle outState);
8 64        void onActivityDestroyed(Activity activity);
9 65    }

OnProvideAssistDataListener接口有个onProvideAssistData方法,为构建Intent.ACTION_ASSIST而存在,可以在其中填入自己的程序状态参数

 1 67    /**
 2 68     * Callback interface for use with {@link Application#registerOnProvideAssistDataListener}
 3 69     * and {@link Application#unregisterOnProvideAssistDataListener}.
 4 70     */
 5 71    public interface OnProvideAssistDataListener {
 6 72        /**
 7 73         * This is called when the user is requesting an assist, to build a full
 8 74         * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
 9 75         * application.  You can override this method to place into the bundle anything
10 76         * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
11 77         * of the assist Intent.
12 78         */
13 79        public void onProvideAssistData(Activity activity, Bundle data);
14 80    }

 构造函数调用父类构造函数

1 82    public Application() {
2 83        super(null);
3 84    }
4 85

onCreate在所有的组件自前被调用,重写的话要调用父类的onCreate;onTerminate不会在量产安卓机器上被调用

 1 86    /**
 2 87     * Called when the application is starting, before any activity, service,
 3 88     * or receiver objects (excluding content providers) have been created.
 4 89     * Implementations should be as quick as possible (for example using
 5 90     * lazy initialization of state) since the time spent in this function
 6 91     * directly impacts the performance of starting the first activity,
 7 92     * service, or receiver in a process.
 8 93     * If you override this method, be sure to call super.onCreate().
 9 94     */
10 95    @CallSuper
11 96    public void onCreate() {
12 97    }
13 98
14 99    /**
15 100     * This method is for use in emulated process environments.  It will
16 101     * never be called on a production Android device, where processes are
17 102     * removed by simply killing them; no user code (including this callback)
18 103     * is executed when doing so.
19 104     */
20 105    @CallSuper
21 106    public void onTerminate() {
22 107    }

onConfigurationChanged,onLowMemory,onTrimMemory会先调用collectComponentCallbacks,然后调用数组里其相应的同名函数,有的会判断其合法性

 1 109    @CallSuper
 2 110    public void onConfigurationChanged(Configuration newConfig) {
 3 111        Object[] callbacks = collectComponentCallbacks();
 4 112        if (callbacks != null) {
 5 113            for (int i=0; i<callbacks.length; i++) {
 6 114                ((ComponentCallbacks)callbacks[i]).onConfigurationChanged(newConfig);
 7 115            }
 8 116        }
 9 117    }
10 118
11 119    @CallSuper
12 120    public void onLowMemory() {
13 121        Object[] callbacks = collectComponentCallbacks();
14 122        if (callbacks != null) {
15 123            for (int i=0; i<callbacks.length; i++) {
16 124                ((ComponentCallbacks)callbacks[i]).onLowMemory();
17 125            }
18 126        }
19 127    }
20 128
21 129    @CallSuper
22 130    public void onTrimMemory(int level) {
23 131        Object[] callbacks = collectComponentCallbacks();
24 132        if (callbacks != null) {
25 133            for (int i=0; i<callbacks.length; i++) {
26 134                Object c = callbacks[i];
27 135                if (c instanceof ComponentCallbacks2) {
28 136                    ((ComponentCallbacks2)c).onTrimMemory(level);
29 137                }
30 138            }
31 139        }
32 140    }
33 141

registerComponentCallbacks,unregisterComponentCallbacks,registerActivityLifecycleCallbacks,unregisterActivityLifecycleCallbacks,registerOnProvideAssistDataListener,unregisterOnProvideAssistDataListener这几个方向相应的向几个ArrayList里add和remove其参数item

 1 142    public void registerComponentCallbacks(ComponentCallbacks callback) {
 2 143        synchronized (mComponentCallbacks) {
 3 144            mComponentCallbacks.add(callback);
 4 145        }
 5 146    }
 6 147
 7 148    public void unregisterComponentCallbacks(ComponentCallbacks callback) {
 8 149        synchronized (mComponentCallbacks) {
 9 150            mComponentCallbacks.remove(callback);
10 151        }
11 152    }
12 153
13 154    public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
14 155        synchronized (mActivityLifecycleCallbacks) {
15 156            mActivityLifecycleCallbacks.add(callback);
16 157        }
17 158    }
18 159
19 160    public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
20 161        synchronized (mActivityLifecycleCallbacks) {
21 162            mActivityLifecycleCallbacks.remove(callback);
22 163        }
23 164    }
24 165
25 166    public void registerOnProvideAssistDataListener(OnProvideAssistDataListener callback) {
26 167        synchronized (this) {
27 168            if (mAssistCallbacks == null) {
28 169                mAssistCallbacks = new ArrayList<OnProvideAssistDataListener>();
29 170            }
30 171            mAssistCallbacks.add(callback);
31 172        }
32 173    }
33 174
34 175    public void unregisterOnProvideAssistDataListener(OnProvideAssistDataListener callback) {
35 176        synchronized (this) {
36 177            if (mAssistCallbacks != null) {
37 178                mAssistCallbacks.remove(callback);
38 179            }
39 180        }
40 181    }

下面是Internal API

1 183    // ------------------ Internal API ------------------

 

Application,Service,Broadcast类

标签:重写   interface   single   cut   uil   类构造   change   clu   base   

原文地址:http://www.cnblogs.com/cascle/p/7731569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!