首页 > 代码库 > Android 从硬件到应用:一步一步向上爬 5 -- 在Frameworks层添硬件服务

Android 从硬件到应用:一步一步向上爬 5 -- 在Frameworks层添硬件服务

Android Frameworks层提供了硬件服务,Android系统APP可以调用这些硬件服务,进而完成硬件的控制,实现应有的功能。接着上一篇,这一篇要在frameworks层为应用提供java接口的硬件服务。cd到frameworks/base/core/java/android/os目录,添加 IGpioService.aidl:

package android.os;  

interface IGpioService {  
void setVal(int val);  
int getVal();  
}  
我们通过setVal去设置LED的亮灭,getVal一直是省略的

打开frameworks/base下的Android.mk,修改LOCAL_SRC_FILES,增加:

	core/java/android/os/IGpioService.aidl \
编译IGpioService.aidl接口:(成功编译源码前提下)

mmm frameworks/base

生成:

Install: out/target/product/generic/system/framework/framework.odex
Install: out/target/product/generic/system/framework/framework.jar
成功后cd到frameworks/base/services/java/com/android/server目录,添加GpioService.java文件:

package com.android.server;  
import android.content.Context;  
import android.os.IGpioService;  
import android.util.Slog;  

public class GpioService extends IGpioService.Stub {  
	private static final String TAG = "GpioService";  
	GpioService() {  
	    init_native();  
	}  
	public void setVal(int val) {  
	    setVal_native(val);  
	}     
	public int getVal() {  
	    return getVal_native();  
	}  
	  
	private static native boolean init_native();  
	private static native void setVal_native(int val);  
	private static native int getVal_native();  
};  
修改当前目录下的SystemServer.java文件,在ServerThread::run中增加GpioService:

            try {
                Slog.i(TAG, "Recognition Service");
                recognition = new RecognitionManagerService(context);
            } catch (Throwable e) {
                reportWtf("starting Recognition Service", e);
            }

            try {
                Slog.i(TAG, "DiskStats Service");
                ServiceManager.addService("diskstats", new DiskStatsService(context));
            } catch (Throwable e) {
                reportWtf("starting DiskStats Service", e);
            }

            try {
                Slog.i(TAG, "Gpio Service");
                ServiceManager.addService("gpio", new GpioService());
            } catch (Throwable e) {
                Slog.e(TAG, "Failure starting Gpio Service", e);
            }
编译GpioService:

mmm frameworks/base/services/java

生成:

Install: out/target/product/generic/system/framework/services.odex
Install: out/target/product/generic/system/framework/services.jar
Frameworks层已经包含了我们编写的硬件服务,应用程序可以通过这些java接口访问硬件服务。

Android 从硬件到应用:一步一步向上爬 5 -- 在Frameworks层添硬件服务