本文基于 MTK 8.1.0
package android.app
interface IIrScanManager {
void getUartData();
void powerOn();
void powerOff();
}
完成后需要在frameworks/base/Android.mk 声明我们需要的aidl文件
LOCAL_SRC_FILES += \
core/java/android/app/IIrScanManager.aidl
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");
}
}
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);
}
}
}
public static final String IRSCAN_SERVICE = "irscan";
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));
}});
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权限
type irscan,system_api_service,system_server_service,service_manager_type;
irscan u:object_r:irscan:s0
(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))
(typeattributeset irscan_26_0 (irscan))
使用 make update-api && make -j8 2>&1 | tee build.log 进行编译
刷完机开机后,adb shell 进入后使用 service list | grep irscan ,如果能显示
irscan: [android.app.IrScanManager]
证明添加成功
IrScanManager manager = getSystemService(Context.IRSCAN_SERVICE);
manager.getUartData();
如果需要在studio中运行,导入编译后的frameworks.jar
路径:out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar