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();
    }
}
相关推荐
cililin2 天前
第4章 文件管理
linux·服务器·网络·操作系统·unix
崎岖Qiu3 天前
【OS笔记04】:进程和线程2-进程控制
笔记·操作系统·os
charlie1145141916 天前
Windows 10系统编程——进程专题:枚举我们进程的状态
c++·windows·学习·操作系统·进程
A-刘晨阳8 天前
Linux安装centos8及基础配置
linux·运维·服务器·操作系统·centos8
你的人类朋友10 天前
【操作系统】说说 x86 和 x64
后端·程序员·操作系统
张红尘10 天前
龙蜥OS8.10配置repo源使用RPM安装Redis8.2
linux·redis·操作系统
鹏大师运维11 天前
麒麟系统中修改 WPS 默认新建文件格式的方法
linux·操作系统·wps·docx·麒麟·word文档·excel文档
Juchecar11 天前
范式与生活范式
操作系统
大模型铲屎官11 天前
【数据结构与算法-Day 35】拓扑排序:从依赖关系到关键路径的完整解析
人工智能·python·深度学习·操作系统·数据结构与算法·关键路径·扩扑排序