AOSP 9.0如何添加系統(tǒng)服務(wù)(SystemService)

這個(gè)demo的功能很簡單,就是輸出一句log.這篇文章只是講了下添加系統(tǒng)服務(wù)的具體操作,沒有講原理,后面不出意外的話應(yīng)該會(huì)寫一篇文章專門講這個(gè),希望不會(huì)鴿.
首先說明添加系統(tǒng)服務(wù)一共分為兩步: 1.自定義系統(tǒng)服務(wù)(HelloService), 2. 注冊系統(tǒng)服務(wù).

1. 自定義系統(tǒng)服務(wù)HelloService

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

  1. 添加 HelloServiceManager.java
  2. 添加 IHelloServiceManager.aidl (可以不加,主要是為了生成3)
  3. 添加 IHelloServiceManager.java( aidl + 2 自動(dòng)生成)
  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 (這個(gè)可以利用AS自動(dòng)生成IHelloServiceManager.java,然后直接添加java文件就行,不用再添加aidl)


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

HelloService.java(注意該文件添加位置與前兩個(gè)不同).

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!");
    }   
}

一個(gè)自定義的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()函數(shù)中添加,啟動(dòng)最早,也可以在startCoreServices()或者startOtherServices()中添加.

private static final String HELLO_SERVICE = "com.android.server.HelloService";
...
//startBootstrapServices()函數(shù)中添加 
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();
        }

以上就是添加系統(tǒng)服務(wù)的全部代碼.

3 編譯源碼 及 驗(yàn)證

編譯只需要兩個(gè)命令.(在source build/envsetup.sh 以及 lunch 后) 首先make update-api 然后再make即可.
寫一個(gè)app然后調(diào)用

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

如果不出意外的話AS會(huì)報(bào)錯(cuò),因?yàn)镾DK里面沒有HelloService 和 Context.HELLO_SERVICE這寫東西. 但其實(shí)系統(tǒng)是有的,只是編譯器不知道罷了, 所以騙過AS就好, 把上面的兩行代碼用反射實(shí)驗(yàn)就好.

最后 點(diǎn)個(gè)贊吧. 關(guān)注下就更好了. 謝謝!

參考文章
android添加service

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,224評(píng)論 0 3
  • 原文鏈接:Android系統(tǒng)服務(wù)(SystemService)簡介 - CSDN博客 什么是SystemServi...
    Walter_Hu閱讀 1,746評(píng)論 1 3
  • 簡介 今天來談?wù)凙ndroid系統(tǒng)service,大家都知道android系統(tǒng)是基于Linux的。只不過是對Lin...
    CyrusChan閱讀 1,782評(píng)論 0 1
  • 上周咖啡館來了一位小伙子,說工作很迷茫,找不到方向,畢業(yè)已經(jīng)3年,換了幾份工作都是做不長久,錢賺不到,能力也得不到...
    6943ad4ff276閱讀 727評(píng)論 0 1
  • 風(fēng)聲嘩,柳樹斜。吹卷枝頭驚老鴉。烏云滿際涯。 千朵花,落萬家。濺起銀珠亂似麻。黃昏云罩紗。 注:泗陽下午四點(diǎn)鐘雨至...
    慶善閱讀 939評(píng)論 13 26

友情鏈接更多精彩內(nèi)容