在 Android 系统源码中自定义系统服务(Custom System Service in AOSP)

该原创文章首发于微信公众号:字节流动

在 Android 系统源码中自定义系统服务(Custom System Service in AOSP)

配置编译环境(Initial AOSP build environment.)

cd AOSP root dir
source build/envsetup.sh
lunch
2

定义 Service 的 AIDL 文件(Define service AIDL file)

path:frameworks/base/core/java/android/os/
Create IHaoHaoService.aidl file

package android.os;

interface IHaoHaoService {

   void setVal(String key,String value);

   String getVal(String key);
}

修改 Android 模块文件(Modify Android.mk file)

path: frameworks/base/Android.mk


......

## READ ME: ########################################################
##
## When updating this list of aidl files, consider if that aidl is
## part of the SDK API.  If it is, also add it to the list below that
## is preprocessed and distributed with the SDK.  This list should
## not contain any aidl files for parcelables, but the one below should
## if you intend for 3rd parties to be able to send those objects
## across process boundaries.
##
## READ ME: ########################################################
LOCAL_SRC_FILES += \

    ......

	core/java/android/os/IPowerManager.aidl \
	core/java/android/os/IRemoteCallback.aidl \
	core/java/android/os/ISchedulingPolicyService.aidl \
	core/java/android/os/IUpdateLock.aidl \
	core/java/android/os/IUserManager.aidl \
	core/java/android/os/IVibratorService.aidl \
	core/java/android/os/IHaoHaoService.aidl \

	......

进行模块编译:

~/aosp/android-6.0.1_r1$ mmm frameworks/base

generate IHaoHaoService.java file.

创建 HaoHaoService 文件(Create HaoHaoService file)

path: frameworks/base/services/core/java/com/android/server/
Create HaoHaoService.java file.

package com.android.server;

import android.util.Slog; 
import android.os.RemoteException;
import android.os.IHaoHaoService;
import java.util.HashMap;

public class HaoHaoService extends IHaoHaoService.Stub {
    private static HashMap<String,String> mCache = null;
    private static final String TAG="HaoHaoService";

    public HaoHaoService() {
    	 mCache = new HashMap<>();
    	 Slog.d(TAG, "HaoHaoService starting.");
    }

   
    public void setVal(String key, String value) throws RemoteException {
        mCache.put(key, value);
    }

    public String getVal(String key) throws RemoteException {
        return mCache.get(key);
    }

}

注册服务(Register service)

Modify frameworks/base/core/java/android/content/Context.java
Add:

     /**
     * Use with {@link #getSystemService} to retrieve a
     * {@link android.os.IHaoHaoService} for accessing the HaoHao service.
     *
     * @see #getSystemService
     * @hide
     */
    public static final String HAOHAO_SERVICE = "haohao";

Modify frameworks/base/services/java/com/android/server/SystemServer.java
Find function startOtherServices()
Add:


            try {
                Slog.i(TAG, "***HaoHao Service***");
                ServiceManager.addService(Context.HAOHAO_SERVICE, new HaoHaoService());
            } catch (Throwable e) {
                Slog.e(TAG, "Failure starting HaoHao Service", e);
            }

Create HaoHaoServiceManager.java file

path: frameworks/base/core/java/android/app/

package android.app;

/**
 * Created by haohao on 17/10/2.
 */
import android.annotation.SdkConstant;
import android.annotation.SystemApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.IHaoHaoService;
import android.util.Log;

public class HaoHaoServiceManager {

    private static final String TAG = "HaoHaoServiceManager";
    private IHaoHaoService mService;

    public HaoHaoServiceManager(Context context, IHaoHaoService service){
        mService = service;
    }

    public void setVal(String key,String value){

        try {

            mService.setVal(key,value);

        } catch(Exception e){

            Log.e(TAG, e.toString());

            e.printStackTrace();
        }

    }
    public String getVal(String key){
        try {

            return mService.getVal(key);

        } catch(Exception e){
            
            Log.e(TAG, e.toString());
            e.printStackTrace();
        }
        return null;
    }
}

Register service in SystemServiceRegistry.java file

path: frameworks/base/core/java/android/app/SystemServiceRegistry.java
Add:

import android.os.IHaoHaoService;
      
        ......

         registerService(Context.HAOHAO_SERVICE, HaoHaoServiceManager.class,
                new CachedServiceFetcher<HaoHaoServiceManager>() {
                    @Override
                    public HaoHaoServiceManager createService(ContextImpl ctx) {
                        IBinder binder = ServiceManager.getService(Context.HAOHAO_SERVICE);
                        IHaoHaoService service = IHaoHaoService.Stub.asInterface(binder);
                        return new HaoHaoServiceManager(ctx, service);
            }});

        ......

Modify SePolicy Build Check

path: external/sepolicy/service.te

Add:

type haohao_service, system_api_service, system_server_service, service_manager_type;

path: external/sepolicy/service_contexts

Add:

haohao u:object_r:haohao_service:s0

service_contexts

accessibility                             u:object_r:accessibility_service:s0
account                                   u:object_r:account_service:s0
activity                                  u:object_r:activity_service:s0
alarm                                     u:object_r:alarm_service:s0
haohao                                    u:object_r:haohao_service:s0
android.security.keystore                 u:object_r:keystore_service:s0
android.service.gatekeeper.IGateKeeperService    u:object_r:gatekeeper_service:s0

Update api and build

~/aosp/android-6.0.1_r1$ make update-api -j8

~/aosp/android-6.0.1_r1$ make -j8

Test service in your Activity

    @Override
    public void onCreate() {
        super.onCreate();

        mHaoHaoServiceManager = (HaoHaoServiceManager)getSystemService(Context.HAOHAO_SERVICE);
        mHaoHaoServiceManager.setVal("haohao", "Android Developer");

联系与交流

我的公众号
我的公众号
我的微信

我的微信

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android AOSP 源码的 device 目录下添加自定义的 framework 模块的步骤如下: 1. 创建新的模块目录: 在 device/{vendor_name}/{device_name}/ 目录下创建一个新的子目录,用于存放自定义的 framework 模块。例如,可以创建一个名为 myframework 的目录: ``` mkdir -p device/{vendor_name}/{device_name}/myframework ``` 2. 添加 Android.mk 文件: 在 myframework 目录下创建一个名为 Android.mk 的文件,用于定义自定义的 framework 模块。例如,可以添加以下代码: ``` LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := myframework LOCAL_MODULE_TAGS := optional LOCAL_MODULE_CLASS := JAVA_LIBRARIES LOCAL_SRC_FILES := MyFramework.java LOCAL_SDK_VERSION := current LOCAL_JAVA_LIBRARIES := core-libart LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4 LOCAL_STATIC_ANDROID_LIBRARIES := libsqlite include $(BUILD_JAVA_LIBRARY) ``` 其: - LOCAL_MODULE 定义了模块名称; - LOCAL_MODULE_TAGS 定义了模块的标签,optional 表示该模块是可选的; - LOCAL_MODULE_CLASS 定义了模块的类型,JAVA_LIBRARIES 表示该模块是一个 Java 库; - LOCAL_SRC_FILES 定义了该模块包含的 Java 源代码文件; - LOCAL_JAVA_LIBRARIES 定义了该模块所依赖的 Java 库; - LOCAL_STATIC_JAVA_LIBRARIES 和 LOCAL_STATIC_ANDROID_LIBRARIES 定义了该模块所依赖的静态库和 Android 库。 3. 添加 Android.bp 文件: 在 myframework 目录下创建一个名为 Android.bp 的文件,用于定义自定义的 framework 模块。例如,可以添加以下代码: ``` java_library { name: "myframework", srcs: ["MyFramework.java"], static_libs: [ "core-libart", "android-support-v4", ], shared_libs: [ "libsqlite", ], sdk_version: "current", installable: true, } ``` 其: - name 定义了模块名称; - srcs 定义了该模块包含的 Java 源代码文件; - static_libs 定义了该模块所依赖的静态库; - shared_libs 定义了该模块所依赖的共享库; - sdk_version 定义了该模块所需的 Android SDK 版本; - installable 定义了该模块是否可安装。 4. 编译 Android 系统: 在编译 Android 系统之前,需要先执行以下命令: ``` source build/envsetup.sh ``` 然后编译 Android 系统: ``` lunch {device_name}-{build_type} make -j{n} ``` 其 {device_name} 是设备名称,{build_type} 是编译类型(如 userdebug),{n} 是编译线程数。 编译完成后,就可以在 Android 系统使用自定义的 framework 模块了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

字节流动

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

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

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

打赏作者

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

抵扣说明:

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

余额充值