Android from hardware to application: Step by Step 5-add hardware services to the Frameworks Layer
The Android Frameworks layer provides hardware services. Android apps can call these hardware services to implement hardware control and functions. Next, in the previous article, this article will provide the java interface hardware service for the application at the frameworks layer. Cd to the frameworks/base/core/java/android/OS directory, add IGpioService. aidl:
package android.os; interface IGpioService { void setVal(int val); int getVal(); }
We use setVal to set the LED light-off. getVal is always omitted.
Open Android. mk under frameworks/base, modify LOCAL_SRC_FILES, and add:
core/java/android/os/IGpioService.aidl \
Compile the IGpioService. aidl interface: (if the source code is compiled successfully)
mmm frameworks/base
Generation:
Install: out/target/product/generic/system/framework/framework.odexInstall: out/target/product/generic/system/framework/framework.jar
After successful cd, go to the frameworks/base/services/java/com/android/server Directory and add the GpioService. java file:
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(); };
Modify the SystemServer. java file in the current directory and add GpioService in ServerThread: run:
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); }
Compile GpioService:
mmm frameworks/base/services/java
Generation:
Install: out/target/product/generic/system/framework/services.odexInstall: out/target/product/generic/system/framework/services.jar
The Frameworks layer already contains the hardware services we have compiled. Applications can access the hardware services through these java interfaces.