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();
    }
}
相关推荐
崎岖Qiu17 小时前
【OS笔记36】:文件存储空间管理(一)- 空闲区表法
笔记·操作系统·存储管理·文件系统·os
柏木乃一2 天前
进程(11)进程替换函数详解
linux·服务器·c++·操作系统·exec
羑悻的小杀马特4 天前
【Linux篇章】穿越网络迷雾:揭开 HTTP 应用层协议的终极奥秘!从请求响应到实战编程,从静态网页到动态交互,一文带你全面吃透并征服 HTTP 协议,打造属于你的 Web 通信利刃!
linux·运维·网络·http·操作系统·网络通信
彩妙不是菜喵5 天前
操作系统中的Linux:进程详解--->(深入浅出)从入门到精通
linux·操作系统
农民真快落5 天前
【操作系统】手撸xv6操作系统——types.h/param.h/memlayout.h/riscv.h/defs.h头文件解析
操作系统·risc-v·嵌入式软件·xv6
小当家.1056 天前
操作系统期末考试基础知识点速成:高频考点与题集精要
考研·操作系统·计算机基础·速成·大学·期末考试
seasonsyy6 天前
为虚拟机分配内存和磁盘容量
windows·操作系统·内存·vmware·磁盘空间
想用offer打牌6 天前
一站式讲清IO多路复用(轻松愉悦版)
后端·面试·操作系统
seasonsyy6 天前
在虚拟机中安装操作系统需要U盘吗?
windows·操作系统·vmware·虚拟机
fakerth6 天前
【OpenHarmony】升级服务组件(UpdateService)
操作系统·openharmony