当前位置:首页 > Android系统在新进程中启动自定义服务过程(startService)的原理分析
??? s.info = info; ???
??? queueOrSendMessage(H.CREATE_SERVICE, s); ??? } ???
??? ...... ??? ??? } ???
??? ...... ??? ??? }
这里调用ActivityThread的queueOrSendMessage将一个CreateServiceData数据放到消息队列中去,并且分开这个消息。注意,这里已经是在上面Step 4创建的新进程中执行了。
Step 14. ActivityThread.queueOrSendMessage
这个函数定义在frameworks/base/core/java/android/app/ActivityThread.java文件中:
view plain
??? public final class ActivityThread { ??? ??? ...... ???
??? private final void queueOrSendMessage(int what, Object obj) { ??? queueOrSendMessage(what, obj, 0, 0); ??? } ???
??? private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
??? synchronized (this) { ??? ......
??? Message msg = Message.obtain(); ??? msg.what = what; ??? msg.obj = obj; ??? msg.arg1 = arg1; ??? msg.arg2 = arg2; ??? mH.sendMessage(msg); ??? } ??? }
???
??? ...... ??? ??? }
这里调用成员变量mH的sendMessage函数进行消息分发。这里的mH的类型为H,它继承于Handler类。
Step 15. H.sendMessage 这个函数继承于
Handle
类的
sendMessage
函数中,定义在
frameworks/base/core/java/android/os/Handler.java文件中。这个函数我们就不看了,有兴趣的读者可以自己研究一下。消息分发以后,就进入到H.handleMessage函数进行处理了。 Step 16. H.handleMessage
这个函数定义在frameworks/base/core/java/android/app/ActivityThread.java文件中:
view plain
??? public final class ActivityThread { ??? ??? ...... ???
??? private final class H extends Handler { ??? ??? ...... ???
??? public void handleMessage(Message msg) { ???
??? ...... ???
??? switch (msg.what) { ???
??? ...... ???
??? case CREATE_SERVICE:
??? handleCreateService((CreateServiceData)msg.obj); ??? break; ???
??? ...... ??? } ???
??? ...... ???
??? } ???
??? ...... ??? ??? } ???
??? ...... ??? ??? }
这里要处理的消息是CREATE_SERVICE,它调用ActivityThread类的handleCreateService成员函数进一步处理。
Step 17. ActivityThread.handleCreateService
这个函数定义在frameworks/base/core/java/android/app/ActivityThread.java文件中:
view plain
??? public final class ActivityThread { ??? ??? ...... ???
??? private final void handleCreateService(CreateServiceData data) {
??? // If we are getting ready to gc after going to the background, well ??? // we are back active so skip it. ??? unscheduleGcIdler(); ???
??? LoadedApk packageInfo = getPackageInfoNoCheck( ??? data.info.applicationInfo); ??? Service service = null; ??? try {
??? java.lang.ClassLoader cl = packageInfo.getClassLoader(); ??? service = (Service) cl.loadClass(data.info.name).newInstance(); ??? } catch (Exception e) {
??? if (!mInstrumentation.onException(service, e)) { ??? throw new RuntimeException(
??? \ + data.info.name ??? + \ + e.toString(), e); ??? }
??? } ???
??? try {
??? if (localLOGV) Slog.v(TAG, \ + data.info.name); ???
??? ContextImpl context = new ContextImpl(); ??? context.init(packageInfo, null, this); ???
??? Application mInstrumentation);
??? context.setOuterContext(service);
??? service.attach(context, this, data.info.name, data.token, app, ??? ActivityManagerNative.getDefault()); ??? service.onCreate();
??? mServices.put(data.token, service); ??? try {
??? ActivityManagerNative.getDefault().serviceDoneExecuting( ??? data.token, 0, 0, 0); ??? } catch (RemoteException e) { ??? // nothing to do. ??? } ???
??? } catch (Exception e) {
??? if (!mInstrumentation.onException(service, e)) { ??? throw new RuntimeException(
??? \ + data.info.name ??? + \ + e.toString(), e); ??? } ??? } ??? } ???
??? ...... ??? ??? }
app
=
packageInfo.makeApplication(false,
这里的data.info.name就是自定义的服务类shy.luo.ashmem.Server了。
Step 18. ClassLoader.loadClass
这一步实现在上面的ActivityThread.handleCreateService函数中:
view plain
共分享92篇相关文档