当前位置:首页 > Android系统在新进程中启动自定义服务过程(startService)的原理分析
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.
共分享92篇相关文档