Android 13 Framework 添加自定义的系统服务CustomService

目的: 添加自定义的系统服务,在自定义的服务中开发定制的API接口和功能,独立于系统核心服务,方便开发和维护。

开发环境:Android 13 MTK平台

涉及修改的文件如下

device/mediatek/sepolicy/base/private/service_contexts
device/mediatek/sepolicy/base/vendor/platform_app.te
device/mediatek/sepolicy/base/vendor/priv_app.te
device/mediatek/sepolicy/base/vendor/service_contexts
device/mediatek/sepolicy/base/vendor/system_app.te
device/mediatek/sepolicy/base/vendor/untrusted_app.te
device/mediatek/sepolicy/base/vendor/untrusted_app_27.te
frameworks/base/Android.bp
frameworks/base/core/api/current.txt
frameworks/base/core/java/android/app/SystemServiceRegistry.java
frameworks/base/core/java/android/content/Context.java
frameworks/base/services/java/com/android/server/SystemServer.java
system/sepolicy/prebuilts/api/33.0/private/service_contexts
system/sepolicy/private/service_contexts
system/sepolicy/private/untrusted_app_30.te
system/sepolicy/public/service.te
frameworks/base/core/java/android/app/CustomServiceManager.java
frameworks/base/core/java/android/os/custom/ICustomService.aidl
frameworks/base/services/core/java/com/android/server/CustomService.java

功能实现:

1.添加自定义服务AIDL文件:ICustomService.aidl,定义接口

路径:frameworks/base/core/java/android/os/custom/ICustomService.aidl

package android.os.custom;

interface ICustomService {
   
    String getTestTime();
}

2.添加自定义服务管理类:CustomServiceManager.java,Context.CUSTOM_SERVICE 是新增服务的标识,见下面的 Context.java的修改。

路径:frameworks/base/core/java/android/app/CustomServiceManager.java

package android.app;


import android.content.Context;
import android.os.custom.ICustomService;
import android.annotation.SystemService;
import android.util.Log;

@SystemService(Context.CUSTOM_SERVICE)
public class CustomServiceManager {
   
    private static final String TAG = "CustomServiceManager";
    ICustomService mService;
	
    public CustomServiceManager(Context context,ICustomService service){
   
        mService=service;
    }
	
	public String getTestTime(){
   
		try{
   
            return mService.getTestTime();
        }catch(Exception e){
   
            Log.d(TAG,"getTestTime e.getMessage()="+e.getMessage());
			return "";
        }
	}
}

3.添加自定义服务实现类:CustomService.java,实现具体的功能。

路径:frameworks/base/services/core/java/com/android/server/CustomService.java

package com.android.server;


import java.lang.*;
import java.util.Date;
import java.util.Locale;

import android.icu.text.SimpleDateFormat;
import android.os.RemoteException;
import android.os.custom.ICustomService;
import android.content.Context;
import android.util.Log;

public class CustomService extends ICustomService.Stub {
   
    private static final String TAG="CustomService";

    private final Context mContext;

    public CustomService

你可能感兴趣的:(Android系统,android)