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

在 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:s0account                                   u:object_r:account_service:s0activity                                  u:object_r:activity_service:s0alarm                                     u:object_r:alarm_service:s0haohao                                    u:object_r:haohao_service:s0android.security.keystore                 u:object_r:keystore_service:s0android.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");

联系与交流

我的公众号

我的微信

更多相关文章

  1. Android(安卓)APP —— “时光摄影” 的搭建手记(一)
  2. mipmap和drawable文件夹的区别
  3. Android中使用ffmpeg库进行音视频开发
  4. 【Android(安卓)Dev Guide - 02】 - Application Fundamentals
  5. 命令行方式创建和编译android应用程序
  6. [Android]aidl命令
  7. Android(安卓)Re-installation failed解决方法
  8. Android数据持久化——五种方式
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android(安卓)Camera使用小结
  2. Android简明开发教程十六:Button 画刷示例
  3. Android(安卓)中文API (68) —— Bluetooth
  4. 面向忙碌开发者的 Android(安卓)知识点收
  5. android 输入法弹出 标题栏不被顶出去
  6. Android系列之Android(安卓)命令行手动编
  7. android横竖屏切换参数
  8. ListView点击效果设置
  9. 安卓中的布局属性详解
  10. Android(安卓)技术要点