怎样实现硬件访问服务
1、JNI 和 HAL
(com_android_server_ledservice.cpp)
注册JNI本地方法
|
|加载
|
hal_led.c : open,read,write
3、修改SystemServer.java:加载C库

在其main方法里面:
public static void main(String[] args){
new SystemServer().run();
}
run(){
System.loadLibrary("android_servers");//对应onload.cpp文件
}run方法中:

JNI文件:注册本地方法com_android_server_LedService.cpp供LedService.java使用

有哪些本地方法
static const JNINativeMethod methods[] = {
{"native_ledOpen","()I",(void *)ledOpen},
{"native_ledClose","()V",(void *)ledClose},
{"native_ledCtrl","(II)I",(void *)ledCtrl},
}注册本地方法:
int register_android_server_LedService(JNIEnv *env)
{
//给LedService 类注册
return
//注册本地方法。
jniRegisterNativeMethods(env,"com/android/server/LedService",methods,NELEM(methods));
}
onload.cpp:被调用
extern "C" jint JNI_On_Load()
{
register_android_server_LedService(env);
}

对于复杂的操作可修改HAl,然后将生成的.so文件放到系统中。
startOtherServices:



这就是个类

调用一些NATive方法:


告诉系统是谁:

注册进去:



4、LedService.java:调用本地方法来操作硬件
package com.android.server;
import android.os.ILedsService;
//类 LedService ,它继承的父类是ILedService.Stub类
public class LedService extends ILedService.Stub{
private static final String Tag = "LedService";
//call native c function to access hardware
public int ledCtrl(int which, int status)
{
return native_ledCtrl(which,status);
}
public LedService(){
native_ledOpen();
}
public int native native_ledCtrl(int which, int status);
}

SystemServer.java中
Slog.i(Tag, “Led Service”);
ServiceManager.addService(“led”, new LedService);//new LedService 创建实例化对象
5、IledService.java(接口文件):给app使用
aidl(Android Interface Definition Language)文件
interface ILedService(){
int ledCtrl(int which, int status);
}需要修改:Android.mk
添加一行:
core/java/android/os/ILedService.aidl\
执行mmm . 命令
生成:IledService.java 文件
6、app使用
=====================
JNI复习:

硬件访问服务:
SystemServer:

因为操作都不一样:











