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

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

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

  • 62 次阅读
  • 3 次下载
  • 2026/1/21 11:44:33

2. implements Watchdog.Monitor, BatteryStatsImpl.Bat

teryCallback { 3.

4. ...... 5.

6. public ComponentName startService(IApplicationThread caller, Intent serv

ice,

7. String resolvedType) {

8. // Refuse possible leaked file descriptors

9. if (service != null && service.hasFileDescriptors() == true) { 10. throw new IllegalArgumentException(\

Intent\); 11. } 12.

13. synchronized(this) {

14. final int callingPid = Binder.getCallingPid(); 15. final int callingUid = Binder.getCallingUid(); 16. final long origId = Binder.clearCallingIdentity(); 17. ComponentName res = startServiceLocked(caller, service, 18. resolvedType, callingPid, callingUid); 19. Binder.restoreCallingIdentity(origId); 20. return res; 21. } 22. } 23.

24. ...... 25. 26. }

这里的参数caller、service和resolvedType分别对应ActivityManagerProxy.startService传进来的三个参数。 Step 2. ActivityManagerService.startServiceLocked 这个函数同样定义在

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java文件中:

view plain

1. public final class ActivityManagerService extends ActivityManagerNative 2. implements Watchdog.Monitor, BatteryStatsImpl.Bat

teryCallback { 3.

4. ...... 5.

6. ComponentName startServiceLocked(IApplicationThread caller, 7. Intent service, String resolvedType, 8. int callingPid, int callingUid) { 9. synchronized(this) { 10. ...... 11.

12. ServiceLookupResult res =

13. retrieveServiceLocked(service, resolvedType, 14. callingPid, callingUid); 15. 16. ...... 17.

18. ServiceRecord r = res.record; 19. 20. ...... 21.

22. if (!bringUpServiceLocked(r, service.getFlags(), false)) { 23. return new ComponentName(\, \); 24. }

25. return r.name; 26. } 27. } 28.

29. ...... 30. 31. }

函数首先通过retrieveServiceLocked来解析service这个Intent,就是解析前面我们在AndroidManifest.xml定义的Service标签的intent-filter相关内容,然后将解析结果放在res.record中,然后继续调用bringUpServiceLocked进一步处理。 Step 3. ActivityManagerService.bringUpServiceLocked 这个函数同样定义在

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java文件中:

view plain

1. public final class ActivityManagerService extends ActivityManagerNative 2. implements Watchdog.Monitor, BatteryStatsImpl.Ba

tteryCallback { 3.

4. ...... 5.

6. private final boolean bringUpServiceLocked(ServiceRecord r, 7. int intentFlags, boolean whileRestarting) { 8.

9. ...... 10.

11. final String appName = r.processName; 12.

13. ...... 14.

15. // Not running -- get it started, and enqueue this service record 16. // to be executed when the app comes up.

17. if (startProcessLocked(appName, r.appInfo, true, intentFlags, 18. \, r.name, false) == null) { 19.

20. ...... 21.

22. return false; 23. } 24.

25. if (!mPendingServices.contains(r)) { 26. mPendingServices.add(r); 27. } 28.

29. return true; 30. 31. } 32.

33. ...... 34. 35. }

这里的appName便是我们前面在AndroidManifest.xml文件定义service标签时指定的android:process属性值了,即“.Server”。

接着调用startProcessLocked函数来创建一个新的进程,以便加载自定义的Service类。最后将这个ServiceRecord保存在成员变量mPendingServices列表中,后面会用到。

Step 4. ActivityManagerService.startProcessLocked 这个函数同样定义在

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java文件中:

view plain

1. public final class ActivityManagerService extends ActivityManagerNative 2. implements Watchdog.Monitor, BatteryStatsImpl.Ba

tteryCallback { 3.

4. ...... 5.

6. private final void startProcessLocked(ProcessRecord app, 7. String hostingType, String hostingNameStr) { 8.

9. ...... 10.

11. try { 12.

13. ...... 14.

15. int pid = Process.start(\,

16. mSimpleProcessManagement ? app.processName : nul

l, uid, uid,

17. gids, debugFlags, null); 18.

19. ...... 20.

21. if (pid == 0 || pid == MY_PID) { 22. 23. ...... 24.

25. } else if (pid > 0) { 26. app.pid = pid; 27. app.removed = false;

28. synchronized (mPidsSelfLocked) { 29. this.mPidsSelfLocked.put(pid, app); 30. ...... 31. } 32. } else { 33. 34. ...... 35. } 36.

37. } catch (RuntimeException e) { 38.

39. ...... 40.

41. } 42.

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

共分享92篇相关文档

文档简介:

2. implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { 3. 4. ...... 5. 6. public ComponentName startService(IApplicationThread caller, Intent service, 7. String resolvedType) { 8. // Refuse possible leaked file descriptors 9. if (service != null &

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价: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