一、 准备
- 用于获取系统服务CPU的Core数量。
- 用于编写apk调用系统接口
二、 增加系统服务
-
frameworks/base/core/java/android/app/ICpuInfoManager.aidl
package android.app; interface ICpuInfoManager { int getCpuCore(); }
-
frameworks/base/services/java/com/android/server/CpuInfoManagerService.java
package com.android.server;import android.app.ICpuInfoManager; import android.content.Context; import android.util.Slog; public class CpuInfoManagerService extends ICpuInfoManager.Stub { private Context mContext; public CpuInfoManagerService(Context context){ mContext = context; Slog.d("CPUINFO","Construct"); } @Override public int getCpuCore(){ return 4; } }
增加系统服务Manager frameworks/base/core/java/android/app/CpuInfoManager.java
package android.app; import android.content.Context; import android.os.RemoteException; import android.util.Slog; public class CpuInfoManager { Context mContext; ICpuInfoManager mService; public CpuInfoManager(Context context,ICpuInfoManager service){ mContext = context; mService = service; } public int getCpuCore(){ if(mService != null){ try{ return mService.getCpuCore(); }catch(RemoteException e){ Slog.e("CPUINFO","RemoteException " + e); } } return -1; } }
-
frameworks/base/Android.mk
LOCAL_SRC_FILES += \ core/java/android/app/ICpuInfoManager.aidl \
-
frameworks/base/core/java/android/content/Context.java
/** * 用于Service的注册、SystemServer中的开机启动以及上层应用的使用 */ public static final String CPU_INFO_SERVICE = "cpuinfo";
-
frameworks/base/core/java/android/app/ContextImpl.java
registerService(CONSUMER_IR_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { return new ConsumerIrManager(ctx); }}); // add register cpuinfo service registerService(CPU_INFO_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { IBinder iBinder = ServiceManager.getService(Context.CPU_INFO_SERVICE); return new CpuInfoManager(ctx, ICpuInfoManager.Stub.asInterface(iBinder)); }});
-
frameworks/base/services/java/com/android/server/SystemServer.java
try { display.systemReady(safeMode, onlyCore); } catch (Throwable e) { reportWtf("making Display Manager Service ready", e); } // add cpuinfo service try{ ServiceManager.addService(Context.CPU_INFO_SERVICE, new CpuInfoManagerService(context)); } catch(Throwable e){ Slog.e("CPUINFO","Failed to start CpuInfoManagerService " + e); }
-
make update-api make -j32
之后更新系统。
三、编写app调用
-
此处导入
out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
即可。 -
调用部分代码:
CpuInfoManager cm = (CpuInfoManager) getSystemService("cpuinfo"); // cpuinfo即Context.CPU_INFO_SERVICE, 不知道为什么,AndroidStudio引入frameworkjar包后,调不到。 Log.d("CPUINFO", "getcpuinfo: " + cm.getCpuCore());
四、调试
-
串口下输入
service list | grep cpuinfo
出现以下信息即为添加系统调用服务成功:
53 cpuinfo: [android.app.ICpuInfoManager]
-
串口下输入
logcat -vthreadtime -s CPUINFO
出现以下打印(cpu core is 4)即为app调用成功:
--------- beginning of /dev/log/system --------- beginning of /dev/log/main 01-01 08:00:04.433 1930 1930 D CPUINFO : Construct 05-18 15:03:26.770 4993 4993 D CPUINFO : cpu core is 4
五、参考
Android AOSP 添加系统服务【aidl接口服务】Java层