Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android Activity 啟動

Android Activity 啟動

編輯:高級開發

每個Activity的創建,都會執行到ActivityThread類中的

  Activity performLaunchActivity(ActivityClIEntRecord r, Intent customIntent) 此類負責創建Activity實例和一些information。並回調一些Activity 的回調函數 onXXX().

  其中調用了 activity.attach(*******)。

  Java代碼

  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,

  Object lastNonConfigurationInstance,

  HashMap lastNonConfigurationChildInstances,

  Configuration config) {

  attachBaseContext(context);

  mWindow = PolicyManager.makeNewWindow(this);

  mWindow.setCallback(this);

  if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {

  mWindow.setSoftInputMode(info.softInputMode);

  }

  mUiThread = Thread.currentThread();

  mMainThread = aThread;

  mInstrumentation = instr;

  mToken = token;

  mIdent = ident;

  mApplication = application;

  mIntent = intent;

  mComponent = intent.getComponent();

  MactivityInfo = info;

  mTitle = title;

  mParent = parent;

  mEmbeddedID = id;

  mLastNonConfigurationInstance = lastNonConfigurationInstance;

  mLastNonConfigurationChildInstances = lastNonConfigurationChildInstances;

  mWindow.setWindowManager(null, mToken, mComponent.flattenToString());

  if (mParent != null) {

  mWindow.setContainer(mParent.getWindow());

  }

  mWindowManager = mWindow.getWindowManager();

  mCurrentConfig = config;

  }

  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,

  Object lastNonConfigurationInstance,

  HashMap lastNonConfigurationChildInstances,

  Configuration config) {

  attachBaseContext(context);

  mWindow = PolicyManager.makeNewWindow(this);

  mWindow.setCallback(this);

  if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {

  mWindow.setSoftInputMode(info.softInputMode);

  }

  mUiThread = Thread.currentThread();

  mMainThread = aThread;

  mInstrumentation = instr;

  mToken = token;

  mIdent = ident;

  mApplication = application;

  mIntent = intent;

  mComponent = intent.getComponent();

  MactivityInfo = info;

  mTitle = title;

  mParent = parent;

  mEmbeddedID = id;

  mLastNonConfigurationInstance = lastNonConfigurationInstance;

  mLastNonConfigurationChildInstances = lastNonConfigurationChildInstances;

  mWindow.setWindowManager(null, mToken, mComponent.flattenToString());

  if (mParent != null) {

  mWindow.setContainer(mParent.getWindow());

  }

  mWindowManager = mWindow.getWindowManager();

  mCurrentConfig = config;

  }

  創建Window ,創建的是PhoneWindow

  Java代碼

  private static final String POLICY_IMPL_CLASS_NAME =

  "com.android.internal.policy.impl.Policy";

  static {

  // Pull in the actual implementation of the policy at run-time

  try {

  Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);

  sPolicy = (IPolicy)policyClass.newInstance();

  } catch (ClassNotFoundException ex) {

  throw new RuntimeException(

  POLICY_IMPL_CLASS_NAME + " could not be loaded", ex);

  接上頁

  } catch (InstantiationException ex) {

  throw new RuntimeException(

  POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);

  } catch (IllegalAccessException ex) {

  throw new RuntimeException(

  POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);

  }

  }

  上面代碼是靜態模塊,類被加載時就被執行。

  public static Window makeNewWindow(Context context) {

  return sPolicy.makeNewWindow(context);

  }

  public PhoneWindow makeNewWindow(Context context) {

  return new PhoneWindow(context);

  }

  private static final String POLICY_IMPL_CLASS_NAME =

  "com.android.internal.policy.impl.Policy";

  static {

  // Pull in the actual implementation of the policy at run-time

  try {

  Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);

  sPolicy = (IPolicy)policyClass.newInstance();

  } catch (ClassNotFoundException ex) {

  throw new RuntimeException(

  POLICY_IMPL_CLASS_NAME + " could not be loaded", ex);

  } catch (InstantiationException ex) {

  throw new RuntimeException(

  POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);

  } catch (IllegalAccessException ex) {

  throw new RuntimeException(

  POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);

  }

  }

  上面代碼是靜態模塊,類被加載時就被執行。

  public static Window makeNewWindow(Context context) {

  return sPolicy.makeNewWindow(context);

  }

  public PhoneWindow makeNewWindow(Context context) {

  return new PhoneWindow(context);

  }

  下面是創建WindowManger

  Java代碼

  mWindow.setWindowManager(null, mToken, mComponent.flattenToString());

  public void setWindowManager(WindowManager wm,

  接上頁

  IBinder aPPToken, String appName) {

  mAppToken = aPPToken;

  mAppName = appName;

  if (wm == null) {

  wm = WindowManagerImpl.getDefault();

  }

  mWindowManager = new LocalWindowManager(wm);

  }

  mWindow.setWindowManager(null, mToken, mComponent.flattenToString());

  public void setWindowManager(WindowManager wm,

  IBinder aPPToken, String appName) {

  mAppToken = aPPToken;

  mAppName = appName;

  if (wm == null) {

  wm = WindowManagerImpl.getDefault();

  }

  mWindowManager = new LocalWindowManager(wm);

  }

  wm = WindowManagerImpl.getDefault();是返回一個默認的WindowManagerImpl 的靜態實例,在android系統中的所有應用程序,都共用一個WindowManagerImpl實例。

  Window 被創建的時候,會執行如下代碼來生成Window Attrivute

  Java代碼

  private final WindowManager.LayoutParams mWindowAttributes =

  new WindowManager.LayoutParams();

  private final WindowManager.LayoutParams mWindowAttributes =

  new WindowManager.LayoutParams();

  下面是更改此屬性:

  Java代碼

  /**

  * Specify custom window attributes. PLEASE NOTE: the

  * layout params you give here should generally be from values previously

  * retrIEved with {@link #getAttributes()}; you probably do not want to

  * blindly create and apply your own, since this will blow away any values

  * set by the framework that you are not interested in.

  *

  * @param a The new window attributes, which will completely override any

  * current values.

  */

  public void setAttributes(WindowManager.LayoutParams a) {

  mWindowAttributes.copyFrom(a);

  if (mCallback != null) {

  mCallback.onWindowAttributesChanged(mWindowAttributes);

  接上頁

  }

  }

  /**

  * Specify custom window attributes. PLEASE NOTE: the

  * layout params you give here should generally be from values previously

  * retrIEved with {@link #getAttributes()}; you probably do not want to

  * blindly create and apply your own, since this will blow away any values

  * set by the framework that you are not interested in.

  *

  * @param a The new window attributes, which will completely override any

  * current values.

  */

  public void setAttributes(WindowManager.LayoutParams a) {

  mWindowAttributes.copyFrom(a);

  if (mCallback != null) {

  mCallback.onWindowAttributesChanged(mWindowAttributes);

  }

  }

  要更改mWindowAttributes此屬性,只能用此方法,並回調。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved