Android 9 Wifi 服务启动流程(1)

时间:2025-01-25 14:15:25

Wifi 服务启动流程

从SystemServer开始分析,以 main 函数作为入口
1、
(路径:\frameworks\base\services\java\com\android\server\)

   /**
     * The main entry point from zygote.
     */
    public static void main(String[] args) {
        new SystemServer().run();  //调用 SystemServer 的 run 函数
    }
    

2、run函数启动服务
(路径:\frameworks\base\services\java\com\android\server\)

private void run() {
	...
		//启动服务
		// Start services.
        try {
            traceBeginAndSlog("StartServices");
            startBootstrapServices();
            startCoreServices();
            startOtherServices(); //WIfI 服务在这里
            SystemServerInitThreadPool.shutdown();
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        } finally {
            traceEnd();
        }
    ...
}

3、startOtherServices
(路径:\frameworks\base\services\java\com\android\server\)

 private void startOtherServices() {
	if (context.getPackageManager().hasSystemFeature(
		       PackageManager.FEATURE_WIFI)) {
	           // Wifi Service must be started first for wifi-related services.
	           traceBeginAndSlog("StartWifi");
	           //WIFI_SERVICE_CLASS = ""
	           mSystemServiceManager.startService(WIFI_SERVICE_CLASS); //开启wifi服务 
	          traceEnd();
	          traceBeginAndSlog("StartWifiScanning");
	          mSystemServiceManager.startService(
	          			"");   //开启wifi扫描服务
	          traceEnd();
	   }
}

4、的startService启动服务
(路径:frameworks\base\services\core\java\com\android\server\ )

/**
* Starts a service by class name.
*
* @return The service instance.
*/
@SuppressWarnings("unchecked")
public SystemService startService(String className) {
    final Class<SystemService> serviceClass;
    try {
         serviceClass = (Class<SystemService>)Class.forName(className); //获取到类名
    } catch (ClassNotFoundException ex) {
      	Slog.i(TAG, "Starting " + className);
            throw new RuntimeException("Failed to create service " + className
                    + ": service class not found, usually indicates that the caller should "
                    + "have called () to check whether the "
                    + "feature is available on this device before trying to start the "
                    + "services that implement it", ex);
    }
      return startService(serviceClass);
}

startService(Class serviceClass)

 public <T extends SystemService> T startService(Class<T> serviceClass) {
	...
	Constructor<T> constructor = serviceClass.getConstructor(Context.class);
	service = constructor.newInstance(mContext);//创建实例	
	...
	startService(service); 启动服务
	...
}

startService(@NonNull final SystemService service)

public void startService(@NonNull final SystemService service) {
	// 将服务注册列表里 统一管理
    mServices.add(service);
    // 应该接收生命周期事件的服务
    // private final ArrayList<SystemService> mServices = new ArrayList<SystemService>();
    ...
    service.onStart(); //wifiService的onStart方法
    ...
}

5、
(路径:frameworks\opt\net\wifi\service\java\com\android\server\wifi\)

public final class WifiService extends SystemService {
	...
	 @Override
    public void onStart() {
        Log.i(TAG, "Registering " + Context.WIFI_SERVICE);
        publishBinderService(Context.WIFI_SERVICE, mImpl);\\发布服务,调用父类的方法
    }
	...
}

6、 将service添加到ServiceManager
(frameworks\base\services\core\java\com\android\server\)

	/**
      * 发布服务,以便其他服务和应用程序可以访问它。
      *
      * @param name 新服务的名称
      * @param service 服务对象
      */
    protected final void publishBinderService(String name, IBinder service) {
        publishBinderService(name, service, false);
    }
     
    /** 发布服务,以便其他服务和应用程序可以访问它。
     *
     * @param name 新服务的名称
     * @param service 服务对象
     * @param allowIsolated 设置为 true 以允许隔离的沙盒进程访问此服务
     */
    protected final void publishBinderService(String name, IBinder service,
            boolean allowIsolated) {
        publishBinderService(name, service, allowIsolated, DUMP_FLAG_PRIORITY_DEFAULT);
    }

	/**
     * 发布服务,以便其他服务和应用程序可以访问它。
     *
     * @param name 新服务的名称
     * @param service 服务对象
     * @param allowIsolated 设置为 true 以允许隔离沙盒进程访问此服务
     * @param dumpPriority 支持作为位掩码的转储优先级
     */
    protected final void publishBinderService(String name, IBinder service,boolean allowIsolated, int dumpPriority) {
    	//将service添加到ServiceManager
        ServiceManager.addService(name, service, allowIsolated, dumpPriority);
    }

这个时候整个WiFi服务已经正常启动可用实现通讯了

ServiceManager提供binder通讯服务

持续更新