基于系统源码 8.1 版本修改添加的
1.添加接口
android8.1/frameworks/base/core/java/android/app/IDemoManager.aidl
package android.app;
interface IDemoManager
{
int getCpuTemperature();
}
2.添加系统服务,实现aidl文件的接口
android8.1/frameworks/base/services/core/java/com/android/server/DemoManagerService.java
package com.android.server;
import android.app.IDemoManager;
import android.content.Context;
import android.util.Slog;
public class DemoManagerService extends IDemoManager.Stub {
private Context mContext;
public DemoManagerService(Context context){
mContext = context;
Slog.d("Demo","Construct");
}
@Override
public int getCpuTemperature(){
return 100;
}
}
3.添加系统服务的Manager
android8.1/frameworks/base/core/java/android/app/DemoManager.java
package android.app;
import android.content.Context;
import android.os.RemoteException;
import android.util.Slog;
public class Demomanager {
Context mContext;
IDemoManager mService;
public DemoManager(Context context,IDemoManager service){
mContext = context;
mService = service;
}
public int getCpuTemperature(){
if(mService != null){
try{
return mService.getCpuTemperature();
}catch(RemoteException e){
Slog.e("Demo","RemoteException "+e);
}
}
return -1;
}
}
4.添加aidl到Makefile src
android8.1/frameworks/base/Android.mk
LOCAL_SRC_FILES += \
core/java/android/app/IDemoManager.aidl \
5.添加DEMO_SERVICE常量
android8.1/frameworks/base/core/java/android/content/Context.java
public static final String DEMO_SERVICE = "demo";
6.将实现的服务注册到系统中去
android8.1/frameworks/base/core/java/android/app/SystemServiceRegistry.java
registerService(Context.ORISLINK_SERVICE,DemoManager.class,
new CachedServiceFetcher<DemoManager>(){
@Override
public DemoManager createService(ContextImpl ctx){
IBinder b = ServiceManager.getService(Context.DEMO_SERVICE);
return new DemoManager(ctx,IDemoManager.Stub.asInterface(b));
} });
7.将注册的服务设置成开机启动服务
android8.1/frameworks/base/services/java/com/android/server/SystemService.java
try{
ServiceManager.addService(Context.DEMO_SERVICE,new DemoManagerService(context));
} catch(Throwable e){
Slog.e("Demo","Failed to start Demo Service "+e);
}
8.编译源码 更新api接口
make update-api
9.修改selinux 添加新服务的权限
/android8.1/device/huawei/angler/sepolicy/service.te
type demo_service, system_api_service, system_server_service, service_manager_type;
android8.1/device/huawei/angler/sepolicy/service_contexts
demo u:object_r:demo_service:s0
10.使用service list 查看服务
angler:/ # service list
Found 136 services:
0 sip: [android.net.sip.ISipService]
1 ims: [com.android.ims.internal.IImsService]
2 carrier_config: [com.android.internal.telephony.ICarrierConfigLoader]
3 phone: [com.android.internal.telephony.ITelephony]
4 isms: [com.android.internal.telephony.ISms]
5 iphonesubinfo: [com.android.internal.telephony.IPhoneSubInfo]
6 simphonebook: [com.android.internal.telephony.IIccPhoneBook]
7 isub: [com.android.internal.telephony.ISub]
8 nfc: [android.nfc.INfcAdapter]
9 telecom: [com.android.internal.telecom.ITelecomService]
10 contexthub: [android.hardware.location.IContextHubService]
11 netd_listener: [android.net.metrics.INetdEventListener]
12 connmetrics: [android.net.IIpConnectivityMetrics]
13 bluetooth_manager: [android.bluetooth.IBluetoothManager]
14 autofill: [android.view.autofill.IAutoFillManager]
15 imms: [com.android.internal.telephony.IMms]
16 media.camera.proxy: [android.hardware.ICameraServiceProxy]
17 media_projection: [android.media.projection.IMediaProjectionManager]
18 launcherapps: [android.content.pm.ILauncherApps]
...
如果服务不存在则可能是 selinux 策略添加失败导致没有权限无法启动,所以需要继续去修改添加selinux策略,本次测试使用的手机是Nexus 5系统版本为8.1 所以修改的策略路径为 device/huawei/angler/sepolicy/service.te service_contexts 。如果是在其他设备上添加系统服务那么这个路径可能会不适用,使用 find -name service.te 查找当前系统文件树下路径。找到与当前设备名相一致的路径进去修改添加策略。
编译后使用 service list 命令 可看到添加的服务。
angler:/ # service list |grep demo
51 demo: [android.app.IDemoManager]
11.编写代码测试
import android.app.DemoManager;
DemoManager om = (DemoManager) getSystemService(Context.DEMO_SERVICE);
Log.d(TAG, "Current temperature is " + om.getCpuTemperature());
[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法