jQuery鼠标指针特效

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();

编译中可能会遇到很多的问题,一定要仔细查看报错日志,不要只知道伸手党!!!
大部分原因可能是因为你添加代码时没有导入相应的包,导致编译找不到类。
可能会遇到的问题,原作
参考

系统服务里面添加一个自定义函数

./frameworks/base/services/core/java/com/android/server/audio/AudioService.java

    private String getAudioFocusPkg = "";
    //请求音频焦点
    public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb,
            IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags,
            IAudioPolicyCallback pcb, int sdk) {
            
        ......

        int ret = mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd,
                clientId, callingPackageName, flags, sdk,
                forceFocusDuckingForAccessibility(aa, durationHint, uid));
        android.util.Log.d("TAG",callingPackageName + " requestAudioFocus ret = "+ret);//add
        if(ret == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)getAudioFocusPkg = callingPackageName;//add
        return ret;
    }

    //放弃音频焦点
    public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa,
            String callingPackageName) {
            
        ......

        if (aa != null && !isValidAudioAttributesUsage(aa)) {
            Log.w(TAG, "Request using unsupported usage.");
            mmi.set(MediaMetrics.Property.EARLY_RETURN, "unsupported usage").record();

            return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
        }
        mmi.record();
        int ret = mMediaFocusControl.abandonAudioFocus(fd, clientId, aa, callingPackageName);
        //add
        android.util.Log.d("TAG",callingPackageName + " abandonAudioFocus ret = "+ret);
        if(ret == AudioManager.AUDIOFOCUS_REQUEST_GRANTED && getAudioFocusPkg.equals(callingPackageName)){
            getAudioFocusPkg = callingPackageName;
        }
        //end
        return ret;
    }
    

    //add a textFunction about audiofocus start
    public String setTextFunction(){
        android.util.Log.d("TAG","to setTextFunction");
        return getAudioFocusPkg;
    }
    //add a textFunction about audiofocus end

./frameworks/base/media/java/android/media/IAudioService.aidl

interface IAudioService {
    ......
    
    String setTextFunction();//add

    ......
}

./frameworks/base/media/java/android/media/AudioManager.java

public class AudioManager {
    ......
    
    public String setTextFunction(){
        final IAudioService service = getService();
        try {
            return service.setTextFunction();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }
}

修改framework/base下面的api要注意更新api/current.txt文件,如何更新?
1:如果修改了Android原有API的 ,需要执行 update frameworks/base/api/current.txt,系统会自动更新这个文件!
切记:不能手动更改api/current.txt文件去更新 !!!

2:如果我们对framework/base/下新增aidl,需要执行 make update-api

Android 更新系统api接口

posted @   僵小七  阅读(1251)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示