With digital technology changing with each passing day, the perfect combination of software and hardware has created the popularity of smart mobile devices. Today, everyone is rushing to iOS and Android systems, which is to a certain extent because these two systems have a variety of application software. Therefore, the relationship between software and hardware can be said to a certain extent that hardware serves software. Hardware engineers develop a hardware device, which naturally lacks software engineers to write drivers for it; the ultimate purpose of the driver is to enable the top-level applications to use the services provided by these hardware to provide users with software functions. For application software on Android system, it is necessary to provide hardware services to the system's Application Frameworks layer. In the previous articles, we focused on the custom hardware service interfaces provided by the Linux kernel layer, hardware abstraction layer and runtime library layer. These interfaces are implemented through C or C++ languages. In this article, we will introduce how to provide Java interface hardware services at the Application Frameworks layer of the Android system.
1. Refer toUbuntu Android HAL writes JNI method to provide JAVA access hardware service interfaceAs shown in the article, prepare the JNI method call layer for the hardware abstraction layer module.
2. In the Android system, hardware services generally run in an independent process to provide services to various applications.Therefore, communication between the application that calls these hardware services and these hardware services needs to be done through a proxy. To do this, we must first define the communication interface. Enter the frameworks/base/core/java/android/os directory and add the interface definition file:
USER-NAME@MACHINE-NAME:~/Android$ cd frameworks/base/core/java/android/os
USER-NAME@MACHINE-NAME:~/Android/frameworks/base/core/java/android/os$ vi
The IHelloService interface is defined:
package ; interface IHelloService { void setVal(int val); int getVal(); }
The IHelloService interface mainly provides the function of equipment and obtaining the value of the hardware register val, which is implemented through the two functions setVal and getVal respectively.
3. Return to the frameworks/base directory, open the file, modify the value of the LOCAL_SRC_FILES variable, and add the source file:
## READ ME: ########################################################
##
## When updating this list of aidl files, consider if that aidl is
## part of the SDK API. If it is, also add it to the list below that
## is preprocessed and distributed with the SDK. This list should
## not contain any aidl files for parcelables, but the one below should
## if you intend for 3rd parties to be able to send those objects
## across process boundaries.
##
## READ ME: ########################################################
LOCAL_SRC_FILES += /
....................................................................
core/java/android/os/ /
core/java/android/os/ /
core/java/android/service/urlrenderer/ /
.....................................................................
4. Compilation interface:
USER-NAME@MACHINE-NAME:~/Android$ mmm frameworks/base
In this way, the corresponding interface will be generated according to it.
5. Enter the frameworks/base/services/java/com/android/server directory and add new files:
package ; import ; import ; import ; public class HelloService extends { private static final String TAG = "HelloService"; HelloService() { 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(); };
HelloService mainly calls the JNI methods init_native, setVal_native and getVal_native (see inUbuntu Android HAL writes JNI method to provide JAVA access hardware service interface1 article) to provide hardware services.
6. Modify the files in the same directory and add the code to load HelloService in the ServerThread::run function:
@Override public void run() { .................................................................................... try { (TAG, "DiskStats Service"); ("diskstats", new DiskStatsService(context)); } catch (Throwable e) { (TAG, "Failure starting DiskStats Service", e); } try { (TAG, "Hello Service"); ("hello", new HelloService()); } catch (Throwable e) { (TAG, "Failure starting Hello Service", e); } ...................................................................................... }
7. Compile HelloService and repackage:
USER-NAME@MACHINE-NAME:~/Android$ mmm frameworks/base/services/java
USER-NAME@MACHINE-NAME:~/Android$ make snod
In this way, the repackaged system image file contains our customized hardware service HelloService in the Application Frameworks layer, and will automatically load HelloService when the system starts. At this time, the application can access Hello hardware services through the Java interface. We will describe in the next article how to write a Java application to call this HelloService interface to access the hardware, so stay tuned.
The above is the implementation method of adding hardware access services to the Android Application Frameworks layer. We will continue to add relevant knowledge in the future. Thank you for your support!