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

本文档介绍了如何在Android 13 MTK平台上添加自定义系统服务CustomService,包括创建AIDL接口文件、服务管理类、服务实现类,以及注册和权限修改等步骤,并提供了使用和验证的说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的: 添加自定义的系统服务,在自定义的服务中开发定制的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 11引入了一些新的框架和API,使得开发者能够更容易地添加自定义系统服务。要创建并调用一个自定义系统服务,你可以按照以下步骤操作: 1. **创建Service组件**: 首先,你需要创建一个新的Service类,继承自`android.app.Service`。例如,假设你创建了一个名为`MyCustomService`的服务: ```java public class MyCustomService extends Service { // 定义必要的生命周期回调函数,如onStartCommand() 和 onBind() @Override public int onStartCommand(Intent intent, int flags, int startId) { // 在这里处理启动请求 return START_STICKY; } @Nullable @Override public IBinder onBind(Intent intent) { // 返回Binder,以便外部应用通过Intent绑定到服务 return new MyCustomBinder(); } private class MyCustomBinder extends Binder { // 提供一些方法供外界应用程序调用 public void performCustomAction(String action) { // 自定义业务逻辑 } } } ``` 2. **添加权限和声明**: 在AndroidManifest.xml中,你需要为服务添加相应的权限,并在<application>标签内声明它: ```xml <uses-permission android:name="android.permission.BIND_SERVICE" /> ... <service android:name=".MyCustomService" android:permission="android.permission.BIND_SERVICE"> <intent-filter> <action android:name="com.example.MY_CUSTOM_ACTION" /> </intent-filter> </service> ``` 3. **在App中调用服务**: 为了在你的应用中调用这个服务,你需要发送一个意图(Intent)。例如,在`MainActivity`或`Fragment`中: ```java Intent serviceIntent = new Intent(this, MyCustomService.class); serviceIntent.setAction("com.example.MY_CUSTOM_ACTION"); startService(serviceIntent); // 或者如果服务支持绑定,你可以这样做: IBinder binder = getApplicationContext().bindService(serviceIntent, new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { ((MyCustomBinder) service).performCustomAction("your_action_here"); } @Override public void onServiceDisconnected(ComponentName name) { // 服务断开连接时调用 } }, Context.BIND_AUTO_CREATE); ``` 注意:确保在API级别低于29的应用中,遵循兼容模式并适配旧版本的Android
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最乱纷飞的code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值