第一范文网 - 专业文章范例文档资料分享平台

Android系统在新进程中启动自定义服务过程(startService)的原理分析

来源:用户分享 时间:2025/6/5 7:44:43 本文由醉歌离人 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

??? 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,

Android系统在新进程中启动自定义服务过程(startService)的原理分析.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c927cx0tk5o1symv1jbrh_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top