本文主要是解析Android中的Application类:
1 /** 2 * Base class for those who need to maintain global application state. You can 3 * provide your own implementation by specifying its name in your 4 * AndroidManifest.xml's <application> tag, which will cause that class 5 * to be instantiated for you when the process for your application/package is 6 * created. 7 * 8 * <p class="note">There is normally no need to subclass Application. In 9 * most situation, static singletons can provide the same functionality in a 10 * more modular way. If your singleton needs a global context (for example 11 * to register broadcast receivers), the function to retrieve it can be 12 * given a {@link android.content.Context} which internally uses 13 * {@link android.content.Context#getApplicationContext() Context.getApplicationContext()} 14 * when first constructing the singleton.</p> 15 */
维持全局应用状态的基类。你可以提供自己的实现,通过在你的AndroidManifest.xml文件中指定它的名字,当你的应用创建的时候,它将被实例化。
一般情况下,没有必要继承Application这个类,大多数情况下,静态单例可以以模块化的方式提供相同的功能。如果你的单例需要一个全局的Context(例如,注册广播接收器的时候),在第一次构建单例的时候可以通过Context.getApplicationContext()获取。
Application中维持三个ArrayList
1 private ArrayList<ComponentCallbacks> mComponentCallbacks = 2 new ArrayList<ComponentCallbacks>();//配置改变、剩余内存低时被调用 3 private ArrayList<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks = 4 new ArrayList<ActivityLifecycleCallbacks>();//Activity的生命周期 5 private ArrayList<OnProvideAssistDataListener> mAssistCallbacks = null; //协助数据(不知道用来干嘛的)
1 public interface ActivityLifecycleCallbacks { 2 void onActivityCreated(Activity activity, Bundle savedInstanceState); 3 void onActivityStarted(Activity activity); 4 void onActivityResumed(Activity activity); 5 void onActivityPaused(Activity activity); 6 void onActivityStopped(Activity activity); 7 void onActivitySaveInstanceState(Activity activity, Bundle outState); 8 void onActivityDestroyed(Activity activity); 9 }
1 /** 2 *在任何activity, service, receiver(除了Content Provider)被创建之前就被调用。这个不应该太耗时,否则会直接影响第一个Activity启动的时间。如果重写这个方法,记得首先调用super.onCreate() 3 */ 4 public void onCreate() { 5 }
1 /** 2 * 这个方法只在模拟器环境中被调用,在实际手机上进程是直接被移除的,不会执行这个方法 3 */ 4 public void onTerminate() { 5 }
其他的代码都很简单,通过注册监听事件,然后在事件发生的时候发送一个消息给注册了监听的对象
1 public void onConfigurationChanged(Configuration newConfig) { 2 Object[] callbacks = collectComponentCallbacks(); 3 if (callbacks != null) { 4 for (int i=0; i<callbacks.length; i++) { 5 ((ComponentCallbacks)callbacks[i]).onConfigurationChanged(newConfig); 6 } 7 } 8 } 9 10 public void onLowMemory() { 11 Object[] callbacks = collectComponentCallbacks(); 12 if (callbacks != null) { 13 for (int i=0; i<callbacks.length; i++) { 14 ((ComponentCallbacks)callbacks[i]).onLowMemory(); 15 } 16 } 17 } 18 19 public void onTrimMemory(int level) { 20 Object[] callbacks = collectComponentCallbacks(); 21 if (callbacks != null) { 22 for (int i=0; i<callbacks.length; i++) { 23 Object c = callbacks[i]; 24 if (c instanceof ComponentCallbacks2) { 25 ((ComponentCallbacks2)c).onTrimMemory(level); 26 } 27 } 28 } 29 } 30 31 public void registerComponentCallbacks(ComponentCallbacks callback) { 32 synchronized (mComponentCallbacks) { 33 mComponentCallbacks.add(callback); 34 } 35 } 36 37 public void unregisterComponentCallbacks(ComponentCallbacks callback) { 38 synchronized (mComponentCallbacks) { 39 mComponentCallbacks.remove(callback); 40 } 41 } 42 43 public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) { 44 synchronized (mActivityLifecycleCallbacks) { 45 mActivityLifecycleCallbacks.add(callback); 46 } 47 } 48 49 public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) { 50 synchronized (mActivityLifecycleCallbacks) { 51 mActivityLifecycleCallbacks.remove(callback); 52 } 53 } 54 55 public void registerOnProvideAssistDataListener(OnProvideAssistDataListener callback) { 56 synchronized (this) { 57 if (mAssistCallbacks == null) { 58 mAssistCallbacks = new ArrayList<OnProvideAssistDataListener>(); 59 } 60 mAssistCallbacks.add(callback); 61 } 62 } 63 64 public void unregisterOnProvideAssistDataListener(OnProvideAssistDataListener callback) { 65 synchronized (this) { 66 if (mAssistCallbacks != null) { 67 mAssistCallbacks.remove(callback); 68 } 69 } 70 } 71 72 /** 73 * @hide 74 */ 75 /* package */ final void attach(Context context) { 76 attachBaseContext(context); 77 mLoadedApk = ContextImpl.getImpl(context).mPackageInfo; 78 } 79 80 /* package */ void dispatchActivityCreated(Activity activity, Bundle savedInstanceState) { 81 Object[] callbacks = collectActivityLifecycleCallbacks(); 82 if (callbacks != null) { 83 for (int i=0; i<callbacks.length; i++) { 84 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityCreated(activity, 85 savedInstanceState); 86 } 87 } 88 } 89 90 /* package */ void dispatchActivityStarted(Activity activity) { 91 Object[] callbacks = collectActivityLifecycleCallbacks(); 92 if (callbacks != null) { 93 for (int i=0; i<callbacks.length; i++) { 94 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityStarted(activity); 95 } 96 } 97 } 98 99 /* package */ void dispatchActivityResumed(Activity activity) { 100 Object[] callbacks = collectActivityLifecycleCallbacks(); 101 if (callbacks != null) { 102 for (int i=0; i<callbacks.length; i++) { 103 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityResumed(activity); 104 } 105 } 106 } 107 108 /* package */ void dispatchActivityPaused(Activity activity) { 109 Object[] callbacks = collectActivityLifecycleCallbacks(); 110 if (callbacks != null) { 111 for (int i=0; i<callbacks.length; i++) { 112 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityPaused(activity); 113 } 114 } 115 } 116 117 /* package */ void dispatchActivityStopped(Activity activity) { 118 Object[] callbacks = collectActivityLifecycleCallbacks(); 119 if (callbacks != null) { 120 for (int i=0; i<callbacks.length; i++) { 121 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityStopped(activity); 122 } 123 } 124 } 125 126 /* package */ void dispatchActivitySaveInstanceState(Activity activity, Bundle outState) { 127 Object[] callbacks = collectActivityLifecycleCallbacks(); 128 if (callbacks != null) { 129 for (int i=0; i<callbacks.length; i++) { 130 ((ActivityLifecycleCallbacks)callbacks[i]).onActivitySaveInstanceState(activity, 131 outState); 132 } 133 } 134 } 135 136 /* package */ void dispatchActivityDestroyed(Activity activity) { 137 Object[] callbacks = collectActivityLifecycleCallbacks(); 138 if (callbacks != null) { 139 for (int i=0; i<callbacks.length; i++) { 140 ((ActivityLifecycleCallbacks)callbacks[i]).onActivityDestroyed(activity); 141 } 142 } 143 } 144 145 private Object[] collectComponentCallbacks() { 146 Object[] callbacks = null; 147 synchronized (mComponentCallbacks) { 148 if (mComponentCallbacks.size() > 0) { 149 callbacks = mComponentCallbacks.toArray(); 150 } 151 } 152 return callbacks; 153 } 154 155 private Object[] collectActivityLifecycleCallbacks() { 156 Object[] callbacks = null; 157 synchronized (mActivityLifecycleCallbacks) { 158 if (mActivityLifecycleCallbacks.size() > 0) { 159 callbacks = mActivityLifecycleCallbacks.toArray(); 160 } 161 } 162 return callbacks; 163 } 164 165 /* package */ void dispatchOnProvideAssistData(Activity activity, Bundle data) { 166 Object[] callbacks; 167 synchronized (this) { 168 if (mAssistCallbacks == null) { 169 return; 170 } 171 callbacks = mAssistCallbacks.toArray(); 172 } 173 if (callbacks != null) { 174 for (int i=0; i<callbacks.length; i++) { 175 ((OnProvideAssistDataListener)callbacks[i]).onProvideAssistData(activity, data); 176 } 177 } 178 }