Android源碼添加自訂系統服務

來源:互聯網
上載者:User

標籤:

本文以一個簡單的例子詳細介紹如何為Android 6.0 系統新增一個系統服務,以及如何使用自訂系統服務。

目標:1.建立一個自訂服務CCCService

        2.APP 開發過程中可以使用 getSystemService("ccc") 擷取 CCCManager 並且調用裡面的函數。

 

 step1 建立aidl檔案

在源碼frameworks/base/core/java/android/os/ 下面新增 一個 ICCCService.aidl

假設我們定義了5個函數,這些函數將會在SystemServer進程執行。

內容如下 

// ICCCService.aidlpackage android.os;// Declare any non-default types here with import statementsinterface ICCCService {    /**     * Demonstrates some basic types that you can use as parameters     * and return values in AIDL.     */   void setVal(String key,String value);   String getVal(String key);   void appendLog(String log);   void clearLog();   String readLog();}

 

step2 建立Service檔案 (CCCService)

在 frameworks/base/services/core/java/com/android/server/ 下面新增一個 CCCService.java 用來實現aidl檔案定義的介面。

內容如下

package com.android.server;import java.io.InputStreamReader;import java.io.LineNumberReader;import java.lang.*;import java.util.HashMap;import android.os.RemoteException;import android.os.ICCCService;/** * * Created by zhuangqianliu on 2016/9/21. */public class CCCService extends ICCCService.Stub {    private static HashMap<String,String> map=new HashMap<>();    private static String inner_log="";    public CCCService() {    }    @Override    public void setVal(String key, String value) throws RemoteException {        map.put(key,value);    }    @Override    public String getVal(String key) throws RemoteException {        return map.get(key);    }    @Override    public void appendLog(String log) throws RemoteException {        inner_log+=log+"\n";    }    @Override    public void clearLog() throws RemoteException {        inner_log="";    }    @Override    public String readLog() throws RemoteException {        return inner_log;    }}

 

step3 將自訂Service 加入到SystemServer 啟動進程

先在 frameworks/base/core/java/android/content/Context.java 中添加一行 

public static final String CCC_SERVICE="ccc";

修改 frameworks/base/services/java/com/android/server/SystemServer.java

在  startOtherServices() 函數 的try模組中增加以下代碼

 try {                Slog.i(TAG, "CCC Service");                ServiceManager.addService(Context.CCC_SERVICE, new CCCService());            } catch (Throwable e) {                Slog.e(TAG, "Failure starting CCC Service", e);            }

最終效果

 

step4 建立Manager,即CCCManager

在frameworks/base/core/java/android/app/ 下建立CCCManager.java 檔案 內容如下

package android.app;/** * Created by liam on 16/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.ICCCService;import android.util.Log;public class CCCManager {    ICCCService mService;    public CCCManager(Context ctx,ICCCService service){        mService=service;    }    public void setVal(String key,String value){        try{            mService.setVal(key,value);        }catch(Exception e){            Log.e("CCCManager",e.toString());            e.printStackTrace();        }    }    public String getVal(String key){        try{            return mService.getVal(key);        }catch(Exception e){            Log.e("CCCManager",e.toString());            e.printStackTrace();        }        return null;    }    public void appendLog(String log){        try{            mService.appendLog(log);        }catch(Exception e){            Log.e("CCCManager",e.toString());            e.printStackTrace();        }    }    public void clearLog(){        try{            mService.clearLog();        }catch(Exception e){            Log.e("CCCManager",e.toString());            e.printStackTrace();        }    }    public String readLog(){        try{            return mService.readLog();        }catch(Exception e){            Log.e("CCCManager",e.toString());            e.printStackTrace();        }        return null;    }}

 

step5 註冊到SystemService

修改frameworks/base/core/java/android/app/SystemServiceRegistry.java

在靜態代碼塊中增加

 registerService(Context.CCC_SERVICE, CCCManager.class,                new CachedServiceFetcher<CCCManager>() {                    @Override                    public CCCManager createService(ContextImpl ctx) {                        IBinder b = ServiceManager.getService(Context.CCC_SERVICE);                        ICCCService service = ICCCService.Stub.asInterface(b);                        return new CCCManager(ctx, service);                    }});

 

step6 修改SePolicy的編譯驗證

修改 /external/sepolicy/service.te

在最後一行添加

type ccc_service, system_api_service, system_server_service, service_manager_type;

然後修改同目錄下 /external/sepolicy/service_contexts 檔案

中間插入一行 

ccc u:object_r:ccc_service:s0

 

 

step7 重新編譯源碼

別忘了先 make update-api。

 

 

Step8 測試 

 

tip: 

可以先建立一個java工程簡單寫一個CCCManager類,匯出jar 在ide中使用provided 依賴。這樣開發過程中就不會報錯。

 

 

java臨時工程如下使用 IDEA建立的java項目

 

Android studio 項目配置

 

 

Android源碼添加自訂系統服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.