安卓版本:4.4
因为项目中用到了,做一下简要笔记。
- 定义aidl文件,如IFlyscaleManager.aidl,放到frameworks/base/core/java/android/flyscale/IFlyscaleManager.aidl
- 定义服务FlyscaleService.java,实现该IFlyscaleManager.aidl,放到frameworks/base/service/java/com/android/server/flyscale/FlyscaleService.java
- 调用FlyscaleService服务,定义FlyscaleManager.java,放到frameworks/base/core/java/android/flyscael/FlyscaleManager.java
- 添加服务,修改frameworks/base/core/java/android/content/Context.java
添加:
/** *@author bianjb add flyscale service */ public static final String FLYSCALE_SERVICE = "flyscale";
修改initAndLoop()方法,添加
//framework/base/services/java/com/android/server/SystemServer.java //bianjb:add flyscale service FlyscaleService flyscaleService = null; //bianjb: add for flyscale service try { if (PowerGuruService.isEnabled()) { Slog.i(TAG, "Flyscale Manager"); flyscaleService = new FlyscaleService(context); ServiceManager.addService(Context.FLYSCALE_SERVICE, flyscaleService); } } catch (Throwable e) { Slog.e(TAG, "Failure FlyscaleService", e); }
- 实例化并注册服务
修改frameworks/base/core/java/android/app/ContextImpl.java
的static代码块中添加
//bianjb:add for flyscale service registerService(FLYSCALE_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(FLYSCALE_SERVICE); IFlyscaleManager service = IFlyscaleManager.Stub.asInterface(b); return new FlyscaleManager(ctx, service); }});
- 编译IFlyscaleService.aidl
修改frameworks/base/Android.mk
添加
#add for flyscale service LOCAL_SRC_FILES += core/java/android/flyscale/IFlyscaleManager.aidl \
之后重新编译即可,在APP调用的方法与调用ActivityManager方法一致。