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();
    }
}
相关推荐
海棠蚀omo2 天前
解读Linux进程的“摩尔斯电码”:信号产生的原理与实践,掌控进程的生死时速
linux·操作系统
ZhengEnCi4 天前
P3H0-Python-os模块完全指南-操作系统接口与文件路径处理利器
python·操作系统
moringlightyn7 天前
Linux---进程状态
linux·运维·服务器·笔记·操作系统·c·进程状态
Live&&learn8 天前
数据结构vs 内存结构
数据结构·操作系统·内存结构
序属秋秋秋10 天前
《Linux系统编程之进程基础》【进程状态】
linux·运维·c语言·c++·笔记·操作系统·进程状态
“愿你如星辰如月”11 天前
Linux:进程间通信
linux·运维·服务器·c++·操作系统
共享家952712 天前
高级IO-poll
开发语言·操作系统·io
帅锅锅00714 天前
Android 源码学习之init进程
android·架构·操作系统
帅锅锅00714 天前
process 类权限详解
android·操作系统
元亓亓亓15 天前
考研408--操作系统--day4--进程同步&互斥&信息量机制
java·数据库·考研·操作系统·408