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();
    }
}
相关推荐
CS创新实验室3 小时前
从穿孔卡片到云原生:批处理系统的不朽演进与核心思想
云原生·操作系统·批处理
水月wwww21 小时前
ubuntu网络连接出错解决办法
linux·运维·计算机网络·ubuntu·操作系统·ubuntu网络连接
酷柚易汛智推官2 天前
Windows 10 停服下的国产化迁移:统信 UOS 工具核心技术深度解析
windows·操作系统·酷柚易汛
梁辰兴3 天前
计算机操作系统:用户层的I/O软件
操作系统·计算机操作系统·用户层·i/o软件
海棠蚀omo4 天前
Linux基础I/O-打开新世界的大门:文件描述符的“分身术”与高级重定向
linux·操作系统
Fuchsia5 天前
Linux软件编程笔记五——进程Ⅰ
linux·c语言·笔记·操作系统·进程
2401_841495645 天前
黑客攻击基础知识
网络·黑客·操作系统·web·计算机结构·应用程序·黑客攻击
gfdgd xi5 天前
GXDE OS 25.2.1 更新了!引入 dtk6,修复系统 bug 若干
linux·运维·ubuntu·操作系统·bug·移植·桌面
东木君_6 天前
芯外拾遗第二篇:编译、工具链、烧录,你真的搞懂了吗?
linux·单片机·操作系统·嵌入式
草帽lufei7 天前
轻松上手WSL安装与使用
linux·前端·操作系统