Android SystemService的启动流程

       SystemServer.java(由init进程起来)之后会调用:

    // Initialize the system context.

    createSystemContext();<strong>//方法一 

    // Create the system service manager.

    mSystemServiceManager = new SystemServiceManager(mSystemContext);

    LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);

    

    //ActivityManagerService 启动

    mActivityManagerService = mSystemServiceManager.startService(//方法二

    ActivityManagerService.Lifecycle.class).getService();

    mActivityManagerService.setSystemServiceManager(mSystemServiceManager);

    

    //方法一

    private void createSystemContext() {

        ActivityThread activityThread = ActivityThread.systemMain();

        mSystemContext = activityThread.getSystemContext();

        mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);

    }

    //方法二

    public <T extends SystemService> T startService(Class<T> serviceClass) {

        final String name = serviceClass.getName();

        Slog.i(TAG, “Starting ” + name);

        // Create the service.

        if (!SystemService.class.isAssignableFrom(serviceClass)) {

            throw new RuntimeException(“Failed to create ” + name

                    + “: service must extend ” + SystemService.class.getName());

        }

        final T service;

        try {

            Constructor<T> constructor = serviceClass.getConstructor(Context.class);

            service = constructor.newInstance(mContext);

        } catch (InstantiationException ex) {

            throw new RuntimeException(“Failed to create service ” + name

                    + “: service could not be instantiated”, ex);

        } catch (IllegalAccessException ex) {

            throw new RuntimeException(“Failed to create service ” + name

                    + “: service must have a public constructor with a Context argument”, ex);

        } catch (NoSuchMethodException ex) {

            throw new RuntimeException(“Failed to create service ” + name

                    + “: service must have a public constructor with a Context argument”, ex);

        } catch (InvocationTargetException ex) {

            throw new RuntimeException(“Failed to create service ” + name

                    + “: service constructor threw an exception”, ex);

        }

        // Register it.

        mServices.add(service);

        // Start it.

        try {

            service.onStart();//方法三,很关键的方法,会把自己添加到SystemManager中

        } catch (RuntimeException ex) {

            throw new RuntimeException(“Failed to start service ” + name

                    + “: onStart threw an exception”, ex);

        }

        return service;

    }

    //方法三

    @Override

    public void onStart() {

        publishBinderService(Context.FINGERPRINT_SERVICE, new FingerprintServiceWrapper());//方法四

        IFingerprintDaemon daemon = getFingerprintDaemon();

        if (DEBUG) Slog.v(TAG, “Fingerprint HAL id: ” + mHalDeviceId);

        listenForUserSwitches();

    }

    //方法四

    /**

     * Publish the service so it is accessible to other services and apps.

     */

    protected final void publishBinderService(String name, IBinder service,

            boolean allowIsolated) {

        ServiceManager.addService(name, service, allowIsolated);

    }

    //authenticate()为什么要用mhander.post 调用startAuthentication()方法,不可以直接调用

    mHandler.post(new Runnable() {

        @Override

        public void run() {

            startAuthentication(token, opId, effectiveGroupId, receiver, flags, restricted);

        }

    });

    原文作者:主要看气质的搬砖者
    原文地址: https://blog.csdn.net/macdroid/article/details/51427944
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞