本文主要是介绍android系列-SystemServer创建服务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
SystemServer会开启很多服务,这些服务的创建流程类似,以Installer为例子
1.startBootstrapServices
//frameworks\base\services\java\com\android\server\SystemServer.javaprivate void startBootstrapServices() {Installer installer = mSystemServiceManager.startService(Installer.class);
}
2.startService
通过反射创建service
//android10\frameworks\base\services\core\java\com\android\server\SystemServiceManager.java@SuppressWarnings("unchecked")public <T extends SystemService> T startService(Class<T> serviceClass) {try {final String name = serviceClass.getName();//要创建的服务的类名final T service;try {//通过反射创建服务Constructor<T> constructor = serviceClass.getConstructor(Context.class);service = constructor.newInstance(mContext);} catch (InstantiationException ex) {//......} catch (IllegalAccessException ex) {//......} catch (NoSuchMethodException ex) {//......} catch (InvocationTargetException ex) {//......}startService(service);return service;} finally {Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);}}
3.startService
调用service的onStart方法
//android10\frameworks\base\services\core\java\com\android\server\SystemServiceManager.java// Services that should receive lifecycle events.private final ArrayList<SystemService> mServices = new ArrayList<SystemService>();//Installer服务继承自SystemServicepublic void startService(@NonNull final SystemService service) {// Register it.mServices.add(service); //添加到ArrayList// Start it.try {service.onStart();//调用service的onStart方法} catch (RuntimeException ex) {//......}}
这篇关于android系列-SystemServer创建服务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!