云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > Android系统在新进程中启动自定义服务过程(startService)的原理分析

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

  • 62 次阅读
  • 3 次下载
  • 2025/7/6 14:45:21

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

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

??? public final class ActivityManagerService extends ActivityManagerNative ??? implements BatteryStatsImpl.BatteryCallback { ??? ??? ...... ??? ??? public ComponentName startService(IApplicationThread caller, Intent service, ??? String resolvedType) { ??? // Refuse possible leaked file de

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com