【android12】 添加自定义系统服务,AIDL实现HAL Service层

1.添加自定义AIDL,定义接口

系统路径:frameworks/base/core/java/android/app/IHelloWorldManager.aidl

package android.app;

interface IHelloWorldManager{
    
    
     boolean initD();
     int addValD(in int a,in int b);
}

2.添加AIDL接口实现类

系统路径:frameworks/base/core/java/android/app/HelloWorldManagerService.java

package com.android.server;

import android.app.IHelloWorldManager;
import android.content.Context;
import android.os.RemoteException;
import android.util.Slog;
import android.annotation.Nullable;
// import com.android.server.SystemService;

public class HelloWorldManagerService extends IHelloWorldManager.Stub {
    
    

    private static String TAG = "HelloWorldManagerService";

    public HelloWorldManagerService(){
    
    
        boolean result = init();
        if(result){
    
    
            Slog.d(TAG, "init success");
        }else{
    
    
            Slog.d(TAG, "init error");
        }
        Slog.d(TAG, "HelloWorldManagerService init result = " +addVal(3, 5));
    }

    @Override
    @Nullable
    public int addValD(int a, int b) throws RemoteException {
    
    
        Slog.d(TAG, "add value - a=" + a + ", b=" + b);
        return addVal(a, b);
    }

    @Override
    @Nullable
    public boolean initD() throws RemoteException {
    
    
        Slog.d(TAG, " init success");
        return init();
    }

    // public static final class Lifecycle extends SystemService {
    
    
    // private final HelloWorldManagerService mService;

    // public Lifecycle(Context context) {
    
    
    // super(context);
    // mService = new HelloWorldManagerService();
    // Log.d(TAG, "Lifecycle create");
    // }

    // @Override
    // public void onStart() {
    
    
    // Log.d(TAG, "Lifecycle onStart start");
    // publishBinderService(Context.HELLOWORLD_SERVICE, mService);
    // Log.d(TAG, "Lifecycle onStart continue");
    // mService.start();
    // Log.d(TAG, "Lifecycle onStart end");
    // }

    // @Override
    // public void onBootPhase(int phase) {
    
    

    // }

    // // @Override
    // // public void onUserStopped(@NonNull TargetUser user) {
    
    
    // // }

    // public HelloWorldManagerService getService() {
    
    
    // return mService;
    // }
    // }

    // private void start() {
    
    
    // }

    private static native boolean init();

    private static native int addVal(int a, int b);

}

3.添加接口管理类HelloWorldManager.java

系统路径:frameworks/base/core/java/android/app/HelloWorldManager.java

// HelloWorldManager.java

// HelloWorldManager.java

package android.app;

import android.annotation.SystemService;

import android.annotation.Nullable;
import android.content.Context;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import android.util.Singleton;
import android.util.Slog;

@SystemService(Context.HELLOWORLD_SERVICE)
public class HelloWorldManager {
    
    
    private static String TAG = "HelloWorldManager";

    /**
     * @hide
     */
    @UnsupportedAppUsage
    public static IHelloWorldManager getService() {
    
    
        return IHelloWorldManagerSingleton.get();
    }


    @UnsupportedAppUsage
    private static final Singleton<IHelloWorldManager> IHelloWorldManagerSingleton = new Singleton<IHelloWorldManager>() {
    
    
        @Override
        protected IHelloWorldManager create() {
    
    
            final IBinder b = ServiceManager.getService(Context.HELLOWORLD_SERVICE);
            final IHelloWorldManager am = IHelloWorldManager.Stub.asInterface(b);
            return am;
        }
    };

    public int addVal(@Nullable int a, @Nullable int b) throws RemoteException {
    
    
        Slog.d(TAG, "a + b = " + getService().addValD(3, 5));
        return getService().addValD(a, b);
    }

    public boolean init() throws RemoteException{
    
    
        return getService().initD();
    }

    /**
     * @hide
     */
    public HelloWorldManager() {
    
    
    }
}


4.在Context.java类添加服务定义

系统路径:frameworks/base/core/java/android/content/Context.java

  public static final String HELLOWORLD_SERVICE = "helloworld";

在这里插入图片描述

5.SystemServiceRegistry

系统路径:frameworks/base/core/java/android/app/SystemServiceRegistry.java

	// 添加位置:
@SystemApi
public final class SystemServiceRegistry {
    
     
    static {
    
    
	
  		 // 添加内容如下
       // 省略一万行
       registerService(Context.HELLOWORLD_SERVICE, HelloWorldManager.class,
                new CachedServiceFetcher<HelloWorldManager>() {
    
    
            @Override
            public HelloWorldManager createService(ContextImpl ctx) {
    
    
                return new HelloWorldManager();
            }});
            
       // 以下省略一万行
       }
}

6.SystemServer

系统路径:frameworks/base/services/java/com/android/server/SystemServer.java

		// 添加位置:startOtherServices方法
		// 添加内容如下
		// 此处一定要记得捕获异常
            t.traceBegin("StartHelloworldService");
            try {
    
    
                 ServiceManager.addService(Context.HELLOWORLD_SERVICE,
                         new HelloWorldManagerService());
            } catch (Throwable e) {
    
    
                Slog.e(TAG, "Failure starting HelloworldService", e);
            }
            t.traceEnd();

推荐参考博文

猜你喜欢

转载自blog.csdn.net/qq_42071369/article/details/131210626