Android应用程序窗口设计框架 一

2020年08月02日 阅读数:56
这篇文章主要向大家介绍Android应用程序窗口设计框架 一,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

在Android系统中,一个Activity对应一个应用程序窗口,任何一个Activity的启动都是由AMS服务和应用程序进程相互配合来完成的。AMS服务统一调度系统中全部进程的Activity启动,而每一个Activity的启动过程则由其所属进程来完成。AMS服务经过realStartActivityLocked函数来通知应用程序进程启动某个Activity:html

frameworks\base\services\java\com\android\server\am\ ActivityStack.javajava

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
final boolean realStartActivityLocked(ActivityRecord r,
         ProcessRecord app, boolean andResume, boolean checkConfig)
         throws RemoteException {
     ...
     //系统参数发送变化,通知Activity
     if (checkConfig) {
         ①Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(mService.mConfiguration,
                 r.mayFreezeScreenLocked(app) ? r.appToken : null );
         mService.updateConfigurationLocked(config, r, false , false );
     }
     //将进程描述符设置到启动的Activity描述符中
     r.app = app;
     app.waitingToKill = null ;
     //将启动的Activity添加到进程启动的Activity列表中
     int idx = app.activities.indexOf(r);
     if (idx < 0 ) {
         app.activities.add(r);
     }
     mService.updateLruProcessLocked(app, true , true );
     try {
         ...
         //通知应用程序进程加载Activity
         ②app.thread.scheduleLaunchActivity( new Intent(r.intent), r.appToken,
                 System.identityHashCode(r), r.info,
                 new Configuration(mService.mConfiguration),
                 r.compat, r.icicle, results, newIntents, !andResume,
                 mService.isNextTransitionForward(), profileFile, profileFd,
                 profileAutoStop);
         ...
     } catch (RemoteException e) {
         ...
     }
     if (mMainStack) {
         mService.startSetupActivityLocked();
     }
     return true ;
}

AMS经过realStartActivityLocked函数来调度应用程序进程启动一个Activity,参数r为即将启动的Activity在AMS服务中的描述符,参数app为Activity运行所在的应用程序进程在AMS服务中的描述符。函数经过IApplicationThread代理对象ApplicationThreadProxy通知应用程序进程启动r对应的Activity,应用程序进程完成Activity的加载等准备工做后,AMS最后启动该Activity。启动Activity的建立等工做是在应用程序进程中完成的,AMS是经过IApplicationThread接口和应用程序进程通讯的。r.appToken 在AMS服务端的类型为Token,是IApplicationToken的Binder本地对象。android

frameworks\base\core\java\android\app\ ActivityThread.javaapp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
         ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
         Bundle state, List<resultinfo> pendingResults,
         List<intent> pendingNewIntents, boolean notResumed, boolean isForward,
         String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
     //将AMS服务传过来的参数封装为ActivityClientRecord对象
     ActivityClientRecord r = new ActivityClientRecord();
     r.token = token;
     r.ident = ident;
     r.intent = intent;
     r.activityInfo = info;
     r.compatInfo = compatInfo;
     r.state = state;
     r.pendingResults = pendingResults;
     r.pendingIntents = pendingNewIntents;
     r.startsNotResumed = notResumed;
     r.isForward = isForward;
     r.profileFile = profileName;
     r.profileFd = profileFd;
     r.autoStopProfiler = autoStopProfiler;
     updatePendingConfiguration(curConfig);
     //使用异步消息方式实现Activity的启动
     queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
}
</intent></resultinfo>

参数token从AMS服务端通过Binder传输到应用程序进程后,变为IApplicationToken的Binder代理对象,类型为IApplicationToken.Proxy,这是由于AMS和应用程序运行在不一样的进程中。less

\

经过queueOrSendMessage函数将Binder跨进程调用转换为应用程序进程中的异步消息处理异步

frameworks\base\core\java\android\app\ ActivityThread.javaide

?
1
2
3
4
5
6
7
8
9
10
11
12
13
private class H extends Handler {
  public void handleMessage(Message msg) {
     switch (msg.what) {
             case LAUNCH_ACTIVITY: {
                 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart" );
                 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
                 r.packageInfo = getPackageInfoNoCheck(r.activityInfo.applicationInfo, r.compatInfo);
                 handleLaunchActivity(r, null );
                 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
             } break ;
         }
     }
}  

LAUNCH_ACTIVITY消息在应用程序主线程消息循环中获得处理,应用程序经过handleLaunchActivity函数来启动Activity。到此AMS服务就完成了Activity的调度任务,将Activity的启动过程彻底交给了应用程序进程来完成。函数

frameworks\base\core\java\android\app\ ActivityThread.java布局

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
     //主线程空闲时会定时执行垃圾回收,主线程当前要完成启动Activity的任务,所以这里先暂停GC
     unscheduleGcIdler();
     if (r.profileFd != null ) {
         mProfiler.setProfiler(r.profileFile, r.profileFd);
         mProfiler.startProfiling();
         mProfiler.autoStopProfiler = r.autoStopProfiler;
     }
     // Make sure we are running with the most recent config.
     ①handleConfigurationChanged( null , null );
     //建立Activity
     ②Activity a = performLaunchActivity(r, customIntent);
     if (a != null ) {
         r.createdConfig = new Configuration(mConfiguration);
         Bundle oldState = r.state;
         //启动Activity
         ③handleResumeActivity(r.token, false , r.isForward);
         ...
     } else {
         ...
     }
}

performLaunchActivity

应用程序进程经过performLaunchActivity函数将即将要启动的Activity加载到当前进程空间来,同时为启动Activity作准备。post

frameworks\base\core\java\android\app\ ActivityThread.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
     ActivityInfo aInfo = r.activityInfo;
     if (r.packageInfo == null ) {
         //经过Activity所在的应用程序信息及该Activity对应的CompatibilityInfo信息从PMS服务中查询当前Activity的包信息
         r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,Context.CONTEXT_INCLUDE_CODE);
     }
     //获取当前Activity的组件信息
     ComponentName component = r.intent.getComponent();
     if (component == null ) {
         component = r.intent.resolveActivity(mInitialApplication.getPackageManager());
         r.intent.setComponent(component);
     }
     if (r.activityInfo.targetActivity != null ) {
         //packageName为启动Activity的包名,targetActivity为Activity的类名
         component = new ComponentName(r.activityInfo.packageName,
                 r.activityInfo.targetActivity);
     }
     //经过类反射方式加载即将启动的Activity
     Activity activity = null ;
     try {
         java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
         ①activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
         StrictMode.incrementExpectedActivityCount(activity.getClass());
         r.intent.setExtrasClassLoader(cl);
         if (r.state != null ) {
             r.state.setClassLoader(cl);
         }
     } catch (Exception e) {
         ...
     }
     try {
         //经过单例模式为应用程序进程建立Application对象
         ②Application app = r.packageInfo.makeApplication( false , mInstrumentation);
         if (activity != null ) {
             //为当前Activity建立上下文对象ContextImpl
             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(mCompatConfiguration);
             //将当前启动的Activity和上下文ContextImpl、Application绑定
             ④activity.attach(appContext, this , getInstrumentation(), r.token,
                     r.ident, app, r.intent, r.activityInfo, title, r.parent,
                     r.embeddedID, r.lastNonConfigurationInstances, config);
             ...
             //调用Activity的OnCreate函数
             ⑤mInstrumentation.callActivityOnCreate(activity, r.state);
             ...
             //将Activity保存到ActivityClientRecord中,ActivityClientRecord为Activity在应用程序进程中的描述符
             r.activity = activity;
             ...
         }
         r.paused = true ;
         //ActivityThread的成员变量mActivities保存了当前应用程序进程中的全部Activity的描述符
         mActivities.put(r.token, r);
     } catch (SuperNotCalledException e) {
         ...
     }
     return activity;
}

在该函数中,首先经过PMS服务查找到即将启动的Activity的包名信息,而后经过类反射方式建立一个该Activity实例,同时为应用程序启动的每个Activity建立一个LoadedApk实例对象,应用程序进程中建立的全部LoadedApk对象保存在ActivityThread的成员变量mPackages中。接着经过LoadedApk对象的makeApplication函数,使用单例模式建立Application对象,所以在android应用程序进程中有且只有一个Application实例。而后为当前启动的Activity建立一个ContextImpl上下文对象,并初始化该上下文,到此咱们能够知道,启动一个Activity须要如下对象:

1) XXActivity对象,须要启动的Activity;

2) LoadedApk对象,每一个启动的Activity都拥有属于自身的LoadedApk对象;

3) ContextImpl对象,每一个启动的Activity都拥有属于自身的ContextImpl对象;

4) Application对象,应用程序进程中有且只有一个实例,和Activity是一对多的关系;

加载Activity类

?
1
2
3
4
5
6
public Activity newActivity(ClassLoader cl, String className,
         Intent intent)
         throws InstantiationException, IllegalAccessException,
         ClassNotFoundException {
     return (Activity)cl.loadClass(className).newInstance();
}

这里经过类反射的方式来加载要启动的Activity实例对象。

LoadedApk构造过程

首先介绍一下LoadedApk对象的构造过程:

frameworks\base\core\java\android\app\ ActivityThread.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
         int flags) {
     synchronized (mPackages) {
         //经过Activity的包名从对应的成员变量中查找LoadedApk对象
         WeakReference<loadedapk> ref;
         if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0 ) {
             ref = mPackages.get(packageName);
         } else {
             ref = mResourcePackages.get(packageName);
         }
         LoadedApk packageInfo = ref != null ? ref.get() : null ;
         if (packageInfo != null && (packageInfo.mResources == null
                 || packageInfo.mResources.getAssets().isUpToDate())) {
             ...
             return packageInfo;
         }
     }
     //若是没有,则为当前Activity建立对应的LoadedApk对象
     ApplicationInfo ai = null ;
     try {
         //经过包名在PMS服务中查找应用程序信息
         ai = getPackageManager().getApplicationInfo(packageName,
                 PackageManager.GET_SHARED_LIBRARY_FILES, UserId.myUserId());
     } catch (RemoteException e) {
         // Ignore
     }
     //使用另外一个重载函数建立LoadedApk对象
     if (ai != null ) {
         return getPackageInfo(ai, compatInfo, flags);
     }
     return null ;
}
</loadedapk>


?
1
2
3
4
5
6
7
8
9
10
11
12
13
public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
         int flags) {
     boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0 ;
     boolean securityViolation = includeCode && ai.uid != 0
             && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
                     ? !UserId.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
                     : true );
     if ((flags&(Context.CONTEXT_INCLUDE_CODE|Context.CONTEXT_IGNORE_SECURITY))
             == Context.CONTEXT_INCLUDE_CODE) {
         ...
     }
     return getPackageInfo(ai, compatInfo, null , securityViolation, includeCode);
}


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
         ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
     //再次从对应的成员变量中查找LoadedApk实例
     synchronized (mPackages) {
         WeakReference<loadedapk> ref;
         if (includeCode) {
             ref = mPackages.get(aInfo.packageName);
         } else {
             ref = mResourcePackages.get(aInfo.packageName);
         }
         LoadedApk packageInfo = ref != null ? ref.get() : null ;
         if (packageInfo == null || (packageInfo.mResources != null
                 && !packageInfo.mResources.getAssets().isUpToDate())) {
             ...
             //构造一个LoadedApk对象
             packageInfo = new LoadedApk( this , aInfo, compatInfo, this , baseLoader,
                         securityViolation, includeCode &&
                         (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0 );
             //保存LoadedApk实例到ActivityThread的相应成员变量中
             if (includeCode) {
                 mPackages.put(aInfo.packageName,
                         new WeakReference<loadedapk>(packageInfo));
             } else {
                 mResourcePackages.put(aInfo.packageName,
                         new WeakReference<loadedapk>(packageInfo));
             }
         }
         return packageInfo;
     }
}
</loadedapk></loadedapk></loadedapk>

frameworks\base\core\java\android\app\LoadedApk.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public LoadedApk(ActivityThread activityThread, ApplicationInfo aInfo,
         CompatibilityInfo compatInfo,
         ActivityThread mainThread, ClassLoader baseLoader,
         boolean securityViolation, boolean includeCode) {
     mActivityThread = activityThread;
     mApplicationInfo = aInfo;
     mPackageName = aInfo.packageName;
     mAppDir = aInfo.sourceDir;
     final int myUid = Process.myUid();
     mResDir = aInfo.uid == myUid ? aInfo.sourceDir
             : aInfo.publicSourceDir;
     if (!UserId.isSameUser(aInfo.uid, myUid) && !Process.isIsolated()) {
         aInfo.dataDir = PackageManager.getDataDirForUser(UserId.getUserId(myUid),
                 mPackageName);
     }
     mSharedLibraries = aInfo.sharedLibraryFiles;
     mDataDir = aInfo.dataDir;
     mDataDirFile = mDataDir != null ? new File(mDataDir) : null ;
     mLibDir = aInfo.nativeLibraryDir;
     mBaseClassLoader = baseLoader;
     mSecurityViolation = securityViolation;
     mIncludeCode = includeCode;
     mCompatibilityInfo.set(compatInfo);
     if (mAppDir == null ) {
         //为应用程序进程建立一个ContextImpl上下文
         if (ActivityThread.mSystemContext == null ) {
             ActivityThread.mSystemContext =
                 ContextImpl.createSystemContext(mainThread);
             ActivityThread.mSystemContext.getResources().updateConfiguration(
                      mainThread.getConfiguration(),
                      mainThread.getDisplayMetricsLocked(compatInfo, false ),
                      compatInfo);
         }
         mClassLoader = ActivityThread.mSystemContext.getClassLoader();
         mResources = ActivityThread.mSystemContext.getResources();
     }
}

从以上LoadedApk的构造函数能够看出,LoadedApk类记录了Activity运行所在的ActivityThread、Activity所在的应用程序信息、Activity的包名、Activity的资源路径、Activity的库路径、Activity的数据存储路径、类加载器和应用程序所使用的资源等信息。

Application构造过程

当Activity为应用程序进程启动的第一个Activity,所以须要构造一个Application对象

frameworks\base\core\java\android\app\LoadedApk.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public Application makeApplication( boolean forceDefaultAppClass,
         Instrumentation instrumentation) {
     //在应用程序进程空间以单例模式建立Application对象
     if (mApplication != null ) {
         return mApplication;
     }
     Application app = null ;
     //获得应用程序的Application类名
     String appClass = mApplicationInfo.className;
     //若是应用程序没用重写Application,则使用Android默认的Application类
     if (forceDefaultAppClass || (appClass == null )) {
         appClass = "android.app.Application" ;
     }
     try {
         java.lang.ClassLoader cl = getClassLoader();
         //为Application实例建立一个上下文对象ContextImpl
         ①ContextImpl appContext = new ContextImpl();
         //初始化上下文
         ②appContext.init( this , null , mActivityThread);
         //建立Application实例对象
         ③app = mActivityThread.mInstrumentation.newApplication(
                 cl, appClass, appContext);
         appContext.setOuterContext(app);
     } catch (Exception e) {
         ...
     }
     mActivityThread.mAllApplications.add(app);
     mApplication = app;
     if (instrumentation != null ) {
         try {
             //调用Application的OnCreate函数
             ④instrumentation.callApplicationOnCreate(app);
         } catch (Exception e) {
             ...
         }
     }
     return app;
}

在应用程序开发过程当中,当咱们重写了Application类后,应用程序加载运行的是咱们定义的Application类,不然就加载运行默认的Application类。从Application对象的构造过程就能够解释为何应用程序启动后首先执行的是Application的OnCreate函数。在实例化Application对象时,一样建立并初始化了一个ContextImpl上下文对象。

ContextImpl构造过程

前面咱们介绍了,每个Activity拥有一个上下文对象ContextImpl,每个Application对象也拥有一个ContextImpl上下文对象,那么ContextImpl对象又是如何构造的呢?

frameworks\base\core\java\android\app\ ContextImpl.java

?
1
2
3
ContextImpl() {
     mOuterContext = this ;
}

ContextImpl的构造过程什么也没干,经过调用ContextImpl的init函数进行初始化

?
1
2
3
final void init(LoadedApk packageInfo,IBinder activityToken, ActivityThread mainThread) {
     init(packageInfo, activityToken, mainThread, null , null );
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
final void init(LoadedApk packageInfo,IBinder activityToken, ActivityThread mainThread,
             Resources container, String basePackageName) {
     mPackageInfo = packageInfo;