AOSP 9.0如何添加系统服务(SystemService)

这个demo的功能很简单,就是输出一句log.这篇文章只是讲了下添加系统服务的具体操作,没有讲原理,后面不出意外的话应该会写一篇文章专门讲这个,希望不会鸽.
首先说明添加系统服务一共分为两步: 1.自定义系统服务(HelloService), 2. 注册系统服务.

1. 自定义系统服务HelloService

文件位置:frameworks/base/core/java/android/app

  1. 添加 HelloServiceManager.java
  2. 添加 IHelloServiceManager.aidl (可以不加,主要是为了生成3)
  3. 添加 IHelloServiceManager.java( aidl + 2 自动生成)
  4. 添加 HelloService.java(文件位置frameworks/base/services/core/java/com/android/server/)
    HelloServiceManager.java
package android.app;
import android.os.RemoteException;
import android.annotation.SystemService;
import android.content.Context;
 
@SystemService(Context.HELLO_SERVICE)
public final class HelloServiceManager{
  private final IHelloServiceManager mService;
    private Context mContext;
  /** 
   * @hide to prevent subclassing from outside of the framework
   */
  HelloServiceManager(Context context,IHelloServiceManager service){
        mContext = context;
    mService = service;
  }
  public void printHello(){
    try{
      mService.printHello();
    }catch (RemoteException ex){
    }   
  }

IHelloServiceManager.aidl (这个可以利用AS自动生成IHelloServiceManager.java,然后直接添加java文件就行,不用再添加aidl)


package android.app;
/**
* System private API for talking with the helloworld service.
* {@hide}
*/
interface IHelloServiceManager{
   void printHello();
}

HelloService.java(注意该文件添加位置与前两个不同).

package com.android.server;
import android.app.IHelloWorldManager;
import android.content.Context;
import android.os.RemoteException;
import android.util.Slog;
import android.content.Context;
import com.android.server.SystemService;
public class HelloService extends IHelloServiceManager.Stub {
    private final static String LOG_TAG = "HelloService";
 
    private final Object mLock = new Object();
 
    private final Context mContext;
    
    HelloService(Context context) {
        mContext = context;
    }   
    @Override
    public void printHello() throws RemoteException {
        Slog.i(LOG_TAG,"Hello,World!");
    }   
}

一个自定义的service就完成了.

2. 注册Service

修改:
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

SystemServiceRegistry.java //以下代码放在static块即可

registerService(Context.HELLO_SERVICE, HelloServiceManager.class,
                new CachedServiceFetcher<HelloServiceManager>() {
            @Override
            public HelloServiceManager createService(ContextImpl ctx) throws ServiceNotFoundException {
                  IBinder binder;
                if (true){//ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
                    binder = ServiceManager.getServiceOrThrow(Context.HELLO_SERVICE);
                } else {
                    binder = ServiceManager.getService(Context.HELLO_SERVICE);
                }
                return new HelloServiceManager(ctx,IHelloServiceManager.Stub.asInterface(binder));
            }});

Context.java(仿照其他service添加)
主要有两处,一处定义HELLO_SERVICE, 另一处StringDef块里.

            ACCOUNT_SERVICE,
             ACTIVITY_SERVICE,
             ALARM_SERVICE,
+            HELLO_SERVICE,
             NOTIFICATION_SERVICE,
             ACCESSIBILITY_SERVICE,
             CAPTIONING_SERVICE,
           ...
           public static final String HELLO_SERVICE = "hello"; 

SystemServer.java
在startBootstrapServices()函数中添加,启动最早,也可以在startCoreServices()或者startOtherServices()中添加.

private static final String HELLO_SERVICE = "com.android.server.HelloService";
...
//startBootstrapServices()函数中添加 
if(true){
            HelloService helloservice = null;
            try{
                traceBeginAndSlog("HelloService");
                helloservice = new HelloService(mSystemContext);
                ServiceManager.addService(Context.HELLO_SERVICE,helloservice);
            }catch(Throwable e){
                Slog.e(TAG, "Starting HelloService failed!!! ", e);
            }
            traceEnd();
        }

以上就是添加系统服务的全部代码.

3 编译源码 及 验证

编译只需要两个命令.(在source build/envsetup.sh 以及 lunch 后) 首先make update-api 然后再make即可.
写一个app然后调用

HelloService hs = (HelloService) getSystemService(Context.HELLO_SERVICE);
hs.printhello();

如果不出意外的话AS会报错,因为SDK里面没有HelloService 和 Context.HELLO_SERVICE这写东西. 但其实系统是有的,只是编译器不知道罢了, 所以骗过AS就好, 把上面的两行代码用反射实验就好.

最后 点个赞吧. 关注下就更好了. 谢谢!

参考文章
android添加service

推荐阅读更多精彩内容