1. 程式人生 > 其它 >Android11 —— 自定義新增一個System Services

Android11 —— 自定義新增一個System Services

怎麼新增一個System Services 系統服務 ,步驟如下:


1.新增ITwwManager.aidl

//frameworks\base\core\java\android\hardware\tww\ITwwManager.aidl
package android.hardware.tww;
/** @hide */
interface ITwwManager {

    String getName();
}

2.新增Context變數

//frameworks\base\core\java\android\content\Context.java
  /** add text tww**/
    public static final String TWW_SERVICE = "tww";

3.編寫系統服務

//frameworks/base/services/core/java/com/android/server/tww/TwwManagerService.java
package com.android.server.tww;

import android.content.Context;
import android.hardware.tww.ITwwManager;

public class TwwManagerService extends ITwwManager.Stub {
    private final Context mContext;

    public TwwManagerService(Context context) {
        super();
        mContext = context;
    }

    @Override
    public String getName() {
        String name = "Tww hello..";
        return name;
    }
}

4.在SystemServer中新增系統服務

/**
     * Starts a miscellaneous grab bag of stuff that has yet to be refactored and organized.
     */
    private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
        t.traceBegin("startOtherServices");

        final Context context = mSystemContext;
        ......
          // add test tww
            t.traceBegin("SystemServer TwwManagerService");
            ServiceManager.addService(Context.TWW_SERVICE, new TwwManagerService(context));
            t.traceEnd();
  {

5.編寫 Manager 類,暴露介面

//frameworks/base/core/java/android/hardware/tww/TwwManager.java

package android.hardware.tww;

import android.os.IBinder;
import android.os.ServiceManager;
import android.hardware.tww.ITwwManager;
import android.content.Context;
import android.os.RemoteException;
import android.compat.annotation.UnsupportedAppUsage;
import android.annotation.Nullable;
import android.os.ServiceManager.ServiceNotFoundException;
import android.annotation.SystemService;

@SystemService(Context.TWW_SERVICE)
public class TwwManager {
    private static TwwManager sInstance;
    private final ITwwManager mService;
    private Context mContext;

    /**
     * @hide
     */
    public TwwManager(ITwwManager iTwwManager) {
        mService = iTwwManager;
    }

    /**
     * Gets an instance of the tww manager.
     *
     * @return The tww manager instance.
     * @hide
     */
    @UnsupportedAppUsage
    public static TwwManager getInstance() {
       // android.util.Log.d("wxl", "tww getInstance");
        synchronized (TwwManager.class) {
            if (sInstance == null) {

                try {
                    IBinder b = ServiceManager.getServiceOrThrow(Context.TWW_SERVICE);
                    sInstance = new TwwManager(ITwwManager.Stub
                            .asInterface(ServiceManager.getServiceOrThrow(Context.TWW_SERVICE)));
                } catch (ServiceNotFoundException e) {
                    throw new IllegalStateException(e);
                }

            }
            return sInstance;
        }
    }

    @Nullable
    public String getName() {
        android.util.Log.d("tww", "TwwManager getName");
        try {
            return mService.getName();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }
}


6.註冊 Manager 類 ,不要忘記匯入相應的包,前面的也是

//frameworks/base/core/java/android/app/SystemServiceRegistry.java
import android.hardware.tww.TWwManager;

 // Not instantiable.
    private SystemServiceRegistry() { }

    static {
        //add test tww
        registerService(Context.TWW_SERVICE, TwwManager.class,
                new CachedServiceFetcher<TwwManager>() {
                    @Override
                    public TwwManager createService(ContextImpl ctx) {
                        android.util.Log.d("tww","SystemServiceRegistry registerService");
                        return TwwManager.getInstance();
                    }});
              ........
  {

最後,呼叫

 mContext = getApplicationContext();
TwwManager mTwwManager = (TwwManager)mContext.getSystemService(Context.TWW_SERVICE);
android.util.Log.d("tww","Name="+ mTwwManager.getName());

問題

記得執行make update-api 更新framework中的介面,執行後,如何檢視是否更新:
\frameworks\base\api\current.txt

package android.hardware.tww {

  public class TwwManager {
    method @Nullable public String getName();
  }

}

package android.hardware.usb {

  public class UsbAccessory implements android.os.Parcelable {
    method public int describeContents();
    method @Nullable public String getDescription();

編譯中可能會遇到很多的問題,一定要仔細檢視報錯日誌,不要只知道伸手黨!!!
大部分原因可能是因為你新增程式碼時沒有匯入相應的包,導致編譯找不到類。
可能會遇到的問題,原作