Android14 系统添加自定义服务

1.定义AIDL

frameworks\base\core\java\android\os\ICustomManager.aidl

java 复制代码
package android.os;
/**
 * {@hide}
 */
interface ICustomManager
{
  String getValue();
}

2.定义服务

frameworks\base\services\core\java\com\android\server\hover\CustomManagerService.java

java 复制代码
package com.android.server.hover;

import com.android.server.SystemService;
import android.content.Context;
import android.util.Log;
import java.util.HashMap;
import android.os.ICustomManager;

public final class CustomManagerService extends ICustomManager.Stub {

    private static final String TAG = "CustomManagerService";
    final Context mContext;

    public CustomManagerService(Context context) {
        mContext = context;
    }

    @Override
    public String getValue() {
        try {
            return "Hello App";
        } catch (Exception e) {
            e.printStackTrace();
            return "call error";
        }
    }
}

3.定义Manager

frameworks\base\core\java\android\os\CustomManager.java

java 复制代码
package android.os;

import android.annotation.SystemService;
import android.content.Context;
import android.util.Log;
import android.os.Handler;
import android.os.SystemProperties;

import java.io.IOException;
import java.io.DataInputStream;

public final class CustomManager {
    private static final String TAG = "CustomManager";
    final Context mContext;
    final ICustomManager mService;

    /**
     * {@hide}
     */
    public CustomManager(Context context, ICustomManager service) {
        mContext = context;
        mService = service;
    }

    public String getValue() {
        try {
            return mService.getValue();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }
}

4.注册服务

frameworks\base\services\java\com\android\server\SystemServer.java

java 复制代码
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
    ....
    CustomManagerService customService = null;
    t.traceBegin("StartCustomManService");
    try {
        if(customService==null){
            customService = new CustomManagerService(context);
        }
        ServiceManager.addService("custom", customService);
    }catch (Throwable e) {
        Slog.e(TAG, "Failure starting CustomManagerService ", e);
    }
    t.traceEnd();
    ....
}    

frameworks\base\core\java\android\app\SystemServiceRegistry.java

java 复制代码
static {
    ...
    registerService("custom", CustomManager.class,
            new CachedServiceFetcher<CustomManager>() {
                @Override
                public CustomManager createService(ContextImpl ctx) throws ServiceNotFoundException {
                    IBinder b = ServiceManager.getServiceOrThrow("custom");
                    ICustomManager service = ICustomManager.Stub.asInterface(b);
                    return new CustomManager(ctx.getOuterContext(), service);
                }
            });
     ...
}

5.忽略lint检查

bp 复制代码
metalava_framework_docs_args = "" +
    ...
    "--api-lint-ignore-prefix com.android. " +
    "--api-lint-ignore-prefix android. " +
    ...

6.Selinux权限

system\sepolicy\prebuilts\api\34.0\public\service.te

erlang 复制代码
...
type custom_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
...

system\sepolicy\prebuilts\api\34.0\private\service_contexts

erlang 复制代码
...
window                                    u:object_r:window_service:s0
custom                                    u:object_r:custom_service:s0
*                                         u:object_r:default_android_service:s0
...

7.系统应用调用服务接口

java 复制代码
package com.ttit.helloapp;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.CustomManager;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomManager mCustomManager = getSystemService(CustomManager.class);
        mCustomManager.getValue();
    }
}
相关推荐
Serene_Dream11 小时前
OS 内存小结
操作系统·内存
程序员一点16 小时前
第3章:首次启动与基础配置
操作系统·openeuler
冰冷的希望20 小时前
【系统】VMware17虚拟机安装黑苹果macOS 15.0详细步骤(保姆级)
macos·操作系统·系统·vmware·虚拟机·黑苹果
请输入蚊子2 天前
«操作系统真像还原» 第二章 编写MBR主引导记录
linux·汇编·操作系统·bochs·操作系统真像还原
添砖java‘’3 天前
线程的互斥与同步
linux·c++·操作系统·线程·信息与通信
燃于AC之乐3 天前
【Linux系统编程】进程控制完全指南:从fork创建、优雅终止到进程等待的全面解析
linux·操作系统·进程控制·进程创建·进程等待·进程终止·fork函数
Trouvaille ~4 天前
【Linux】Linux线程概念与控制(四):glibc源码剖析与实现原理
linux·运维·服务器·c++·操作系统·glibc·线程控制
_OP_CHEN4 天前
【Linux系统编程】(二十四)深入 Ext2 块组内部:inode、数据块与目录的底层工作机制
linux·操作系统·文件系统·c/c++·inode·块组·数据块映射
番茄灭世神4 天前
Linux从入门到进阶第一章
linux·计算机·操作系统
燃于AC之乐5 天前
【Linux系统编程】进程地址空间完全指南:页表、写时拷贝与虚拟内存管理
linux·操作系统·虚拟内存·进程地址空间