Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Window、PhoneWindow、WindowManager、Activity學習心得 第一彈

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

編輯:關於Android編程

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


閱讀本文,你首先需要理解 Context 上下文環境,Window 窗口的准確含義。 那麼,接下來,我們首先從Activity的啟動開始。(本文重點不在啟動詳細信息@

Android應用程序的Activity啟動過程簡要介紹和學習計劃

)。 我們需要知道Activity有顯式啟動和隱式啟動兩種,但是不管是什麼啟動方式。我們要知道借助於應用程序框架層的ActivityManagerService服務進程。 目錄(源碼目錄/frameworks/base/services/java/com/android/server/am/ActivityManagerService.java) 以後我將用 Android = 源碼目錄 (默認為Android4.4) 在Android應用程序框架層中,ActivityManagerService是一個非常重要的接口,它不但負責啟動Activity和Service,還負責管理Activity和Service。

Android應用程序框架層中的ActivityManagerService啟動Activity的過程大致如下圖所示:

\

具體啟動過程不再做過多贅述,我們這裡只需要知道通過ActivityManagerService把這個啟動Activity的操作轉發給ActivityThread,ActivityThread通過ClassLoader導入相應的Activity類,然後把它啟動起來。

目錄(Android4.4/frameworks/base/core/java/android/app/ActivityThread.java)

那麼,我們下一步進入到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中

 

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;
	}

	......
}

以上關於ActivityThread 啟動Activity,如果還有什麼不明白的請查看 @Android應用程序啟動過程源代碼分析
緊接著上一步,我們跟蹤到Activity的attach 目錄(Android 4.4 /frameworks/base/core/java/android/app/Activity.java)
final void attach(Context context,ActivityThread aThread,
 Instrumentation instr, IBinder token,Application application, 
Intent intent, ActivityInfo info, CharSequence title,Application application,
 Intent intent, ActivityInfo info, CharSequence title,Configuration config) 
{
attach(context, aThread, instr, token, 0, application,intent, info, title, 
parent, id,lastNonConfigurationInstances, 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,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();     ......}
走到這裡,我們終於看到了我們的Window 我們知道,在Android中,每一個Activity組件都有一個關聯的Window對象,用來描述一個應用程序窗口。 每一個應用程序窗口內部又包含有一個View對象,用來描述應用程序窗口的視圖。應用程序窗口視圖是真正用來實現UI內容和布局的,也就是說,每一個Activity組件的UI內容和布局都是通過與其所關聯的一個Window對象的內部的一個View對象來實現的 目錄(Android 4.4/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中 目錄(Android 4.4 /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();
}
}

這裡 我們開始創建繼承了Window的PhoneWindow 在這裡,如果我們想繼續往下分析,我們必須知道 \
一、PhoneWindow實現了Window 二、
應用程序窗口內部所包含的視圖對象的實際類型為DecorView。DecorView類繼承了View類,是作為容器(ViewGroup)來使用的






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