Application,Service,Broadcast类

弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述Application,Service,Broadcast类相关的知识,希望能为你提供帮助。
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 48private ArrayList< ComponentCallbacks> mComponentCallbacks = 2 49new ArrayList< ComponentCallbacks> (); 3 50private ArrayList< ActivityLifecycleCallbacks> mActivityLifecycleCallbacks = 4 51new ArrayList< ActivityLifecycleCallbacks> (); 5 52private ArrayList< OnProvideAssistDataListener> mAssistCallbacks = null;

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

内部接口ActivityLifecycleCallbacks,有onActivity Created,Started,Resumed,Paused,Stopped,SaveInstanced,Destroyed这几个方法
1 57public interface ActivityLifecycleCallbacks { 2 58void onActivityCreated(Activity activity, Bundle savedInstanceState); 3 59void onActivityStarted(Activity activity); 4 60void onActivityResumed(Activity activity); 5 61void onActivityPaused(Activity activity); 6 62void onActivityStopped(Activity activity); 7 63void onActivitySaveInstanceState(Activity activity, Bundle outState); 8 64void 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 71public 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 79public void onProvideAssistData(Activity activity, Bundle data); 14 80}

  构造函数调用父类构造函数
1 82public Application() { 2 83super(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 96public 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 106public void onTerminate() { 22 107}

onConfigurationChanged,onLowMemory,onTrimMemory会先调用collectComponentCallbacks,然后调用数组里其相应的同名函数,有的会判断其合法性
1 109@CallSuper 2 110public void onConfigurationChanged(Configuration newConfig) { 3 111Object[] callbacks = collectComponentCallbacks(); 4 112if (callbacks != null) { 5 113for (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 120public void onLowMemory() { 13 121Object[] callbacks = collectComponentCallbacks(); 14 122if (callbacks != null) { 15 123for (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 130public void onTrimMemory(int level) { 23 131Object[] callbacks = collectComponentCallbacks(); 24 132if (callbacks != null) { 25 133for (int i=0; i< callbacks.length; i++) { 26 134Object c = callbacks[i]; 27 135if (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 142public void registerComponentCallbacks(ComponentCallbacks callback) { 2 143synchronized (mComponentCallbacks) { 3 144mComponentCallbacks.add(callback); 4 145} 5 146} 6 147 7 148public void unregisterComponentCallbacks(ComponentCallbacks callback) { 8 149synchronized (mComponentCallbacks) { 9 150mComponentCallbacks.remove(callback); 10 151} 11 152} 12 153 13 154public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) { 14 155synchronized (mActivityLifecycleCallbacks) { 15 156mActivityLifecycleCallbacks.add(callback); 16 157} 17 158} 18 159 19 160public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) { 20 161synchronized (mActivityLifecycleCallbacks) { 21 162mActivityLifecycleCallbacks.remove(callback); 22 163} 23 164} 24 165 25 166public void registerOnProvideAssistDataListener(OnProvideAssistDataListener callback) { 26 167synchronized (this) { 27 168if (mAssistCallbacks == null) { 28 169mAssistCallbacks = new ArrayList< OnProvideAssistDataListener> (); 29 170} 30 171mAssistCallbacks.add(callback); 31 172} 32 173} 33 174 34 175public void unregisterOnProvideAssistDataListener(OnProvideAssistDataListener callback) { 35 176synchronized (this) { 36 177if (mAssistCallbacks != null) { 37 178mAssistCallbacks.remove(callback); 38 179} 39 180} 40 181}

下面是Internal API
1 183// ------------------ Internal API ------------------

【Application,Service,Broadcast类】 

    推荐阅读