一、 准备
- 编译好的Android4.4源码
用于添加系统服务,用于获取CPU的Core数量。 - AndroidStudio工具
用于编写apk调用系统接口
二、 添加系统服务
- 添加aidl接口
frameworks/base/core/java/android/app/ICpuInfoManager.aidl
package android.app;
interface ICpuInfoManager
{
int getCpuCore();
}
- 添加系统服务,实现aidl文件的接口
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;
}
}
- 将aidl文件添加至Android.mk参与编译
frameworks/base/Android.mk
LOCAL_SRC_FILES += \
core/java/android/app/ICpuInfoManager.aidl \
- .添加Context中的Service常量
frameworks/base/core/java/android/content/Context.java
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);
}});
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);
}
try{
ServiceManager.addService(Context.CPU_INFO_SERVICE, new CpuInfoManagerService(context));
} catch(Throwable e){
Slog.e("CPUINFO","Failed to start CpuInfoManagerService " + e);
}
- 更新api接口并且编译源码
make update-api
make -j32
之后更新系统。
三、编写app调用
- AndroidStudio中引入AOSP的jar包
此处导入out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar 即可。 - 编写app测试
调用部分代码:
CpuInfoManager cm = (CpuInfoManager) getSystemService("cpuinfo");
Log.d("CPUINFO", "getcpuinfo: " + cm.getCpuCore());
四、调试
- 查看服务是否启动
串口下输入
service list | grep cpuinfo
出现以下信息即为添加系统调用服务成功:
53 cpuinfo: [android.app.ICpuInfoManager]
- logcat查看app是否调用成功
串口下输入
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层
|