android 8.1添加系统服务

本文基于 MTK 8.1.0

1.编写aidl文件
  • frameworks/base/core/java/android/app/IIrScanManager.aidl
package android.app

interface IIrScanManager {
   void getUartData();
   void powerOn();
   void powerOff();
}
2.注册aidl文件

完成后需要在frameworks/base/Android.mk 声明我们需要的aidl文件

LOCAL_SRC_FILES += \
      core/java/android/app/IIrScanManager.aidl
3.添加实现服务service
  • frameworks/base/services/core/java/com/android/server/IrScanManagerService.java
package com.android.server

import android.app.IIrScanManager;
import android.content.Context;
import andorid.util.Log;

public class IrScanManagerService extends IIrScanManager.Stud {
	private static final String TAG = "IrScanManagerService ";
	private Context mContext;
	public IrScanManagerService (Context context){
		mContext = context;
	}

	@Override
	public void getUartData(){
		Log.d(TAG,"getUartData");
	}
	
	@Override
	public void powerOn(){
		Log.d(TAG,"powerOn");
	}

	@Override
	public void powerOff(){
		Log.d(TAG,"powerOff");
	}
}
4.添加管理manager
  • frameworks/base/core/java/android/app/IrScanManager.java
package android.app

import android.content.Context;
import android.util.Log;

public class IrScanManager {
	private static final String TAG = "IrScanManager ";
	private Context mContext;
	private IIrScanManager mService;
	
	public IrScanManager (Context context, IIrScanManager service){
		mContext = context;
		mService = service;
	}

	public void getUartData(){
		try {
			mService.getUartData();
		} catch (RemoteException e){
			Log.d(TAG,"get data failur :"+e);
		}
	}
	
	public void powerOn(){
		try {
			mService.powerOn();
		} catch (RemoteException e){
			Log.d(TAG,"powerOn failur :"+e);
		}
	}
	
	public void powerOff(){
		try {
			mService.powerOff();
		} catch (RemoteException e){
			Log.d(TAG,"powerOff failur :"+e);
		}
	}
}
5.在Context.java添加常量
  • frameworks/base/core/java/android/content/Context.java
public static final String IRSCAN_SERVICE = "irscan";
6. 注册系统服务
  • frameworks/base/core/java/android/app/SystemServiceRegistry.java
registerService(Context.IRSCAN_SERVICE,IrScanManager.class,
		new CachedServiceFetcher<IrScanManager>(){
	@Override
	public IrScanManager createService(ContextImpl ctx){
		IBinder b = ServiceManager.getService(Context.IRSCAN_SERVICE);
		return new IrScanManager(ctx,IIrScanManager.Stub.asInterface(b));
	}});

7.开机启动
  • frameworks/base/services/java/com/android/server/SystemServer.java
traceBeginAndSlog("StartIrScanManagerService);
try {
	IrScanManagerService irscanService = new IrScanManagerService(context);
	ServiceManager.addService(Context.IRSCAN_SERVICE,irscanService );
} catch (Throwable e){
	reportWtf("start IrScanManagerService",e);
}
traceEnd();

我在8.1系统上添加系统服务,还需要修改selinux权限

8.添加selinux权限
  • device/mediatek/sepolicy/bsp/plat_private/service.te
type irscan,system_api_service,system_server_service,service_manager_type;
  • device/mediatek/sepolicy/bsp/plat_private/service_contexts
irscan u:object_r:irscan:s0 
  • device/mediatek/sepolicy/bsp/prebuilts/api/26.0/nonplat_sepolicy.cil
(typeattribute isrscan_26_0)
(roletype object_r irscan_26_0)
...

(typeattributeset system_api_service(... irscan_26_0))
(typeattributeset system_server_service(... irscan_26_0))
(typeattributeset service_manager_type(... irscan_26_0))
  • device/mediatek/sepolicy/bsp/private/compat/26.0/26.0.cil
(typeattributeset  irscan_26_0 (irscan))

使用 make update-api && make -j8 2>&1 | tee build.log 进行编译

9.检查服务是否添加成功

刷完机开机后,adb shell 进入后使用 service list | grep irscan ,如果能显示
irscan: [android.app.IrScanManager]
证明添加成功

10.测试
IrScanManager manager = getSystemService(Context.IRSCAN_SERVICE);
manager.getUartData();

如果需要在studio中运行,导入编译后的frameworks.jar
路径:out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar

你可能感兴趣的:(系统修改,8.1添加系统服务)