Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Window、PhoneWindow、WindowManager 與 Activity 學習心得(第一彈)

編輯:關於 Android 程式設計

閱讀本文前,你需要先理解 Context 上下文環境與 Window 窗口的準確含義。那麼,我們就從 Activity 的啟動開始講起。

Activity 的啟動流程

Activity 有顯式啟動與隱式啟動兩種方式,但不論哪種,都離不開應用程序框架層的 ActivityManagerService 服務進程(源碼位於 frameworks/base/services/java/com/android/server/am/ActivityManagerService.java,本文以 Android 4.4 源碼目錄為準)。

在框架層中,ActivityManagerService 是一個非常重要的接口,它既負責啟動也負責管理 Activity 和 Service。其啟動 Activity 的大致流程如下圖:

簡而言之,ActivityManagerService 會把啟動 Activity 的操作轉發給 ActivityThread,後者通過 ClassLoader 導入相應的 Activity 類並將其啟動起來。

ActivityThread 的處理

進入 ActivityThread 執行 handleLaunchActivity:

 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) { ......... Activity a = performLaunchActivity(r, customIntent); if (a != null) { ...... handleResumeActivity(r.token, false, r.isForward,!r.activity.mFinished && !r.startsNotResumed); ....... } ..... }

而在 performLaunchActivity 中,主要完成幾件事:收集 Activity 的 Package 與 Component 信息,通過 ClassLoader 把 Activity 加載進來,根據 AndroidManifest.xml 創建 Application,最後調用 activity.attach 正式開始窗口創建。

 public final class ActivityThread { ...... private final Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { ActivityInfo aInfo = r.activityInfo; if (r.packageInfo == null) { r.packageInfo = getPackageInfo(aInfo.applicationInfo, Context.CONTEXT_INCLUDE_CODE); } ComponentName component = r.intent.getComponent(); if (component == null) { component = r.intent.resolveActivity( mInitialApplication.getPackageManager()); r.intent.setComponent(component); } if (r.activityInfo.targetActivity != null) { component = new ComponentName(r.activityInfo.packageName, r.activityInfo.targetActivity); } //以上 為收集Activity的Package 和 Component 信息 Activity activity = null; try { java.lang.ClassLoader cl = r.packageInfo.getClassLoader(); activity = mInstrumentation.newActivity( cl, component.getClassName(), r.intent); r.intent.setExtrasClassLoader(cl); if (r.state != null) { r.state.setClassLoader(cl); } } catch (Exception e) { ...... } //通過ClassLoade 根據收集的信息把你的Activity加載進來 try { Application app = r.packageInfo.makeApplication(false, mInstrumentation); //根據AndroidManifest.xml配置文件中的Application標簽的信息來創建 ...... if (activity != null) { ContextImpl appContext = new ContextImpl(); appContext.init(r.packageInfo, r.token, this); appContext.setOuterContext(activity); CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager()); Configuration config = new Configuration(mConfiguration); ...... activity.attach(appContext, this, getInstrumentation(), r.token, r.ident, app, r.intent, r.activityInfo, title, r.parent, r.embeddedID, r.lastNonConfigurationInstance, r.lastNonConfigurationChildInstances, config); //這裡正式開始我們的窗口創建 if (customIntent != null) { activity.mIntent = customIntent; } r.lastNonConfigurationInstance = null; r.lastNonConfigurationChildInstances = null; activity.mStartedActivity = false; int theme = r.activityInfo.getThemeResource(); if (theme != 0) { activity.setTheme(theme); } activity.mCalled = false; mInstrumentation.callActivityOnCreate(activity, r.state); ...... r.activity = activity; r.stopped = true; if (!r.activity.mFinished) { activity.performStart(); r.stopped = false; } if (!r.activity.mFinished) { if (r.state != null) { mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state); } } if (!r.activity.mFinished) { activity.mCalled = false; mInstrumentation.callActivityOnPostCreate(activity, r.state); if (!activity.mCalled) { throw new SuperNotCalledException( "Activity " + r.intent.getComponent().toShortString() + " did not call through to super.onPostCreate()"); } } } r.paused = true; mActivities.put(r.token, r); } catch (SuperNotCalledException e) { ...... } catch (Exception e) { ...... } return activity; } ...... }

Activity 的 attach 方法

跟蹤到 Activity 的 attach 方法(frameworks/base/core/java/android/app/Activity.java),在這裡我們終於看到了 Window 的身影——mWindow 通過 PolicyManager.makeNewWindow 創建,並綁定 Callback 與 WindowManager:

 final void attach(Context context, ActivityThread aThread, Instrumentation instr, IBinder token, int ident,Application application, Intent intent, ActivityInfo info,CharSequence title, Activity parent, String id,NonConfigurationInstances lastNonConfigurationInstances, Configuration config) { ..... mWindow = PolicyManager.makeNewWindow(this); mWindow.setCallback(this); ...... //基本信息初始化 ....... mWindow.setWindowManager( (WindowManager)context.getSystemService(Context.WINDOW_SERVICE), mToken, mComponent.flattenToString(), (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0); if (mParent != null) { mWindow.setContainer(mParent.getWindow()); } mWindowManager = mWindow.getWindowManager(); ......}

我們知道,在 Android 中,每個 Activity 組件都關聯一個 Window 對象來描述應用程序窗口,而每個窗口內部又包含一個 View 對象來描述視圖。真正實現 UI 內容與布局的,正是與 Activity 關聯的 Window 對象內部的這個 View。

PolicyManager 與 Policy

PolicyManager 負責創建具體的 Window 實現(frameworks/base/core/java/com/android/internal/policy/PolicyManager.java):

 public final class PolicyManager { private static final String POLICY_IMPL_CLASS_NAME = "com.android.internal.policy.impl.Policy"; private static final IPolicy sPolicy; static { try{ Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME); sPolicy = (IPolicy)policyClass.newInstance(); }catch(....){ ....... } } public static Window makeNewWindow(Context context) { return sPolicy.makeNewWindow(context); } public static LayoutInflater makeNewLayoutInflater(Context context) { return sPolicy.makeNewLayoutInflater(context); } public static WindowManagerPolicy makeNewWindowManager() { return sPolicy.makeNewWindowManager(); } }

而 Policy 中則真正 new 出具體的 PhoneWindow(frameworks/base/policy/src/com/android/internal/policy/impl/Policy.java):

 public class Policy implements IPolicy { private static final String[] preload_classes = { "com.android.internal.policy.impl.PhoneLayoutInflater", "com.android.internal.policy.impl.PhoneWindow", ..... }; static { for (String s : preload_classes) { try{ Class.forName(s); }catch (ClassNotFoundException ex) { } } public Window makeNewWindow(Context context) { return new PhoneWindow(context); } public LayoutInflater makeNewLayoutInflater(Context context) { return new PhoneLayoutInflater(context); } public WindowManagerPolicy makeNewWindowManager() { return new PhoneWindowManager(); } }

關鍵結論

一、PhoneWindow 實現了 Window;

二、應用程序窗口內部所包含的視圖對象的實際類型為 DecorView。DecorView 繼承自 View 類,作為容器(ViewGroup)來使用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved