Added hardware access Service for Android application frameworks layer on Ubuntu (lao Luo study note 5)
Source: Internet
Author: User
<span id="Label3"></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">With the rapid development of digital technology today, the perfect combination of software and hardware has created the popularity of smart mobile Devices. The popularity of iOS and Android today is partly due to the variety of applications on both Systems. therefore, the relationship between software and hardware, to a certain extent, can be said that the hardware is for software Services. Hardware engineers developed a hardware device that is less natural for software engineers to write drivers for, and the ultimate purpose of the driver is to enable top-level applications to use the services provided by these hardware to provide software functionality to Users. For the application software on the Android system, it is to provide the hardware service on the application frameworks layer of the System. In the previous articles, we highlighted the custom hardware service interfaces provided by the Linux kernel layer, the hardware abstraction layer, and the runtime library layer, all implemented through the C or C + + Language. In this article, we will show you how to provide hardware services for the Java interface on the application frameworks layer of the Android System.</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">I. Refer to the article on the Java Access Hardware service interface in Ubuntu for authoring the Jni method for the Android hardware abstraction Layer (HAL) module, and prepare the Jni method call layer for the hardware abstraction layer Module.</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">Two. in android, <strong><span style="color:rgb(0,128,0)">hardware services are typically run in a separate process</span></strong> that serves a variety of applications. therefore, the communication between applications that invoke these hardware services and those hardware services needs to be done through a proxy. To do this, we need to define the communication interface First. Go to the Frameworks/base/core/java/android/os directory and add the <span style="font-size:15px"><u><u><strong><span style="color:rgb(255,0,0)"><u>ihelloservice.aidl</u></span></strong></u></u></span> interface Definition file:</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>[email protected]:~/android$ CD Frameworks/base/core/java/android/os</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>[email protected]:~/android/frameworks/base/core/java/android/os$ VI ihelloservice.aidl</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong> </strong>Ihelloservice.aidl defines the Ihelloservice interface:</p></p><pre style="font-family:‘Courier New‘; margin:5px 8px; padding:5px"><pre style="font-family:‘Courier New‘; margin:5px 8px; padding:5px">package <span>android.os; </span> <span style="color:rgb(0,0,255)">Interface</span> <span>ihelloservice { </span><span style="color:rgb(0,0,255)">void</span> setval (<span style="color:rgb(0,0,255)">int</span> <span> val); </span> <span style="color:rgb(0,0,255)">int</span> <span>getval (); </span></pre></pre><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">The Ihelloservice interface mainly provides the function of the <strong><span style="color:rgb(255,0,0)">device</span></strong> and the <strong><span style="color:rgb(255,0,255)">value of obtaining the Val of the hardware register</span></strong> , respectively, by the Setval and Getval two functions.</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">Three. return to the Frameworks/base directory, open the Android.mk file, Modify the value of the Local_src_files variable, and add the Ihelloservice.aidl source file:</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"># READ ME: ########################################################</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">##</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"># # When updating this list of Aidl files, consider If-aidl is</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"># # Part of the SDK API. If It is, also add it to the list below that</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"># are preprocessed and distributed with the SDK. This list should</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"># # contain any aidl files for parcelables, but the one below should</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"># # If you intend for 3rd parties to be able to send those objects</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"># # Across Process Boundaries.</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">##</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"># READ ME: ########################################################</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">Local_src_files + =/</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">....................................................................</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">core/java/android/os/ivibratorservice.aidl/</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>core/java/android/os/ihelloservice.aidl/</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">core/java/android/service/urlrenderer/iurlrendererservice.aidl/</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">.....................................................................</p></p>Four. Compile the Ihelloservice.aidl interface:<strong><strong>[email protected]:~/android$ mmm frameworks/base</strong></strong><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">In this way, the corresponding Ihelloservice.stub interface is generated based on the Ihelloservice.aidl.</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">Five. Enter the Frameworks/base/services/java/com/android/server directory and add the Helloservice.java file:</p></p><pre style="font-family:‘Courier New‘; margin:5px 8px; padding:5px"><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)"></span> package</span><span><span>com.android.server; Service Pack</span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Import</span></span><span><span>android.content.Context; Import Context</span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Import</span></span><span><span>android.os.IHelloService; Import Hello interface</span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Import</span></span><span><span>android.util.Slog; Import log Log</span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)"></span> public</span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">class</span></span>HelloService<span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">extends</span></span><span><span>Ihelloservice.stub {</span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Private</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Static</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Final</span></span>String TAG = "helloservice"<span><span>; The private part of the class HelloService () {init_native (); } </span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)"></span> public</span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">void</span></span>Setval (<span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">int</span></span><span><span>Val) {//public part setval_native (val); } </span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)"></span> public</span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">int</span></span><span><span>getval () {</span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">return</span></span><span><span>getval_native (); } </span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Private</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Static</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">native</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Boolean</span></span><span><span>init_native (); </span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Private</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Static</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">native</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">void</span></span>Setval_native (<span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">int</span></span><span><span>val); </span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Private</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Static</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">native</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">int</span></span><span><span>getval_native (); }; </span></span></pre><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">HelloService is primarily done by invoking the Jni method init_native, setval_native, and Getval_native ( See the article "java Access hardware service interface" provided by Ubuntu for the Android hardware abstraction Layer (HAL) module, which provides hardware services.</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">Six. Modify the Systemserver.java file of the same directory and add the code to load the HelloService in the Serverthread::run function:</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">@Override</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">public void Run () {</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">....................................................................................</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">try {</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">SLOG.I (TAG, "diskstats Service");</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">Servicemanager.addservice ("diskstats", New Diskstatsservice (context));</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">} catch (throwable E) {</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">SLOG.E (TAG, "Failure starting diskstats Service", e);</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">}</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>try {</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>SLOG.I (TAG, "Hello Service");</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>Servicemanager.addservice ("hello", New HelloService ());</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>} catch (throwable E) {</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>SLOG.E (TAG, "Failure starting Hello Service", e);</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>}</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">......................................................................................</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">}</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px">Seven. compile helloservice and repack system.img:</p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>[email protected]:~/android$ mmm Frameworks/base/services/java</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong>[email protected]:~/android$ make Snod</strong></p></p><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><p style="margin:10px auto; font-family:verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:14px"><strong></strong> This way, the repackaged system.img system image file contains our custom hardware service HelloService in the application frameworks layer, and when the system starts, Load HelloService automatically. At this point, the application can access the Hello hardware service 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 please look forward to it.</p></p><p><p>Added hardware access Service for Android application frameworks layer on Ubuntu (lao Luo study note 5)</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service