在 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");










