发布时间 : 星期三 文章Android系统在新进程中启动自定义服务过程(startService)的原理分析更新完毕开始阅读
??? public final class ActivityManagerService extends ActivityManagerNative ??? implements BatteryStatsImpl.BatteryCallback { ???
??? ...... ???
??? public ComponentName startService(IApplicationThread caller, Intent service,
??? String resolvedType) {
??? // Refuse possible leaked file descriptors
??? if (service != null && service.hasFileDescriptors() == true) { ??? throw new IllegalArgumentException(\descriptors passed in Intent\); ??? } ???
??? synchronized(this) {
??? final int callingPid = Binder.getCallingPid(); ??? final int callingUid = Binder.getCallingUid(); ??? final long origId = Binder.clearCallingIdentity(); ??? ComponentName res = startServiceLocked(caller, service, ??? resolvedType, callingPid, callingUid); ??? Binder.restoreCallingIdentity(origId); ??? return res; ??? } ??? } ???
??? ...... ??? ??? }
Watchdog.Monitor,
这里的参数caller、service和resolvedType分别对应
ActivityManagerProxy.startService传进来的三个参数。
Step 2. ActivityManagerService.startServiceLocked 这文件中:
view plain
个函数同样定义在
frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
??? public final class ActivityManagerService extends ActivityManagerNative ??? implements BatteryStatsImpl.BatteryCallback {
Watchdog.Monitor,
???
??? ...... ???
??? ComponentName startServiceLocked(IApplicationThread caller, ??? Intent service, String resolvedType, ??? int callingPid, int callingUid) { ??? synchronized(this) { ??? ...... ???
??? ServiceLookupResult res =
??? retrieveServiceLocked(service, resolvedType, ??? callingPid, callingUid); ??? ??? ...... ???
??? ServiceRecord r = res.record; ??? ??? ...... ???
??? if (!bringUpServiceLocked(r, service.getFlags(), false)) { ??? return new ComponentName(\, \); ??? }
??? return r.name; ??? } ??? } ???
??? ...... ??? ??? }
函数首先通过retrieveServiceLocked来解析service这个Intent,就是解析前面我们在AndroidManifest.xml定义的Service标签的intent-filter相关内容,然后将解析结果放在res.record中,然后继续调用bringUpServiceLocked进一步处理。
Step 3. ActivityManagerService.bringUpServiceLocked 这文件中:
view plain
个函数同样定义在
frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
??? public final class ActivityManagerService extends ActivityManagerNative ??? implements
Watchdog.Monitor,
BatteryStatsImpl.BatteryCallback { ???
??? ...... ???
??? private final boolean bringUpServiceLocked(ServiceRecord r, ??? int intentFlags, boolean whileRestarting) { ???
??? ...... ???
??? final String appName = r.processName; ???
??? ...... ???
??? // Not running -- get it started, and enqueue this service record ??? // to be executed when the app comes up.
??? if (startProcessLocked(appName, r.appInfo, true, intentFlags, ??? \, r.name, false) == null) { ???
??? ...... ???
??? return false; ??? } ???
??? if (!mPendingServices.contains(r)) { ??? mPendingServices.add(r); ??? } ???
??? return true; ??? ??? } ???
??? ...... ??? ??? }
这里的appName便是我们前面在AndroidManifest.xml文件定义service标签时指定的android:process属性值了,即“.Server”。
接着调用startProcessLocked函数来创建一个新的进程,以便加载自定义的Service类。最后将这个ServiceRecord保存在成员变量mPendingServices列表中,后面会用到。 Step 4. ActivityManagerService.startProcessLocked
这文件中:
view plain
个函数同样定义在
frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
??? public final class ActivityManagerService extends ActivityManagerNative ??? implements BatteryStatsImpl.BatteryCallback { ???
??? ...... ???
??? private final void startProcessLocked(ProcessRecord app, ??? String hostingType, String hostingNameStr) { ???
??? ...... ???
??? try { ???
??? ...... ???
??? int pid = Process.start(\,
??? mSimpleProcessManagement ? app.processName : null, uid, uid,
??? gids, debugFlags, null); ???
??? ...... ???
??? if (pid == 0 || pid == MY_PID) { ??? ??? ...... ???
??? } else if (pid > 0) { ??? app.pid = pid; ??? app.removed = false;
??? synchronized (mPidsSelfLocked) { ??? this.mPidsSelfLocked.put(pid, app); ??? ...... ??? } ??? } else { ??? ??? ...... ??? }
Watchdog.Monitor,