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.jarFrameworks层已经包含了我们编写的硬件服务,应用程序可以通过这些java接口访问硬件服务。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。