Test the hardware services of the Application Frameworks layer for the built-in Java application of the Android system on Ubuntu...

This article is reproduced from: http://blog.csdn.net/luoshengyang/article/details/6580267

we are at Android The purpose of adding hardware services to the system is to allow the APP of the application layer to pass Java interface to access hardware services. So, how does the APP access the hardware services provided by the Application Frameworks layer through the Java interface? In this article, we will add a built-in application to the application layer of the Android system. This built-in application obtains the specified service through the ServiceManager interface, and then obtains hardware services through this service.

1. Reference Adding hardware access services to the Application Frameworks layer of the Android system on Ubuntu In this article, define your own hardware service HelloService in the Application Frameworks layer, and provide the IHelloService interface to provide access services.

2. In order to facilitate development, we can use the Android SDK to develop Android applications in the IDE environment. After the development is completed, transplant the program source code to the Android source code project directory. It is very convenient to use Eclipse's Android plug-in ADT to create an Android project, which is not described here. You can refer to other information on the Internet. The project name is Hello, and the main file is as follows:

The main program is src/shy/luo/hello/Hello.java:

 1 package shy.luo.hello;
 2 
 3 import shy.luo.hello.R;
 4 import android.app.Activity;
 5 import android.os.ServiceManager;
 6 import android.os.Bundle;
 7 import android.os.IHelloService;
 8 import android.os.RemoteException;
 9 import android.util.Log;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.widget.Button;
13 import android.widget.EditText;
14 
15 public class Hello extends Activity implements OnClickListener {
16     private final static String LOG_TAG = "shy.luo.renju.Hello";
17     
18     private IHelloService helloService = null;
19 
20     private EditText valueText = null;
21     private Button readButton = null;
22     private Button writeButton = null;
23     private Button clearButton = null;
24     
25     /** Called when the activity is first created. */
26     @Override
27     public void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.main);
30 
31     helloService = IHelloService.Stub.asInterface(
32         ServiceManager.getService("hello"));
33         
34         valueText = (EditText)findViewById(R.id.edit_value);
35         readButton = (Button)findViewById(R.id.button_read);
36         writeButton = (Button)findViewById(R.id.button_write);
37         clearButton = (Button)findViewById(R.id.button_clear);
38 
39     readButton.setOnClickListener(this);
40     writeButton.setOnClickListener(this);
41     clearButton.setOnClickListener(this);
42         
43         Log.i(LOG_TAG, "Hello Activity Created");
44     }
45     
46     @Override
47     public void onClick(View v) {
48         if(v.equals(readButton)) {
49         try {
50                 int val = helloService.getVal();
51                 String text = String.valueOf(val);
52                 valueText.setText(text);
53         } catch (RemoteException e) {
54             Log.e(LOG_TAG, "Remote Exception while reading value from device.");
55         }        
56         }
57         else if(v.equals(writeButton)) {
58         try {
59                 String text = valueText.getText().toString();
60                 int val = Integer.parseInt(text);
61             helloService.setVal(val);
62         } catch (RemoteException e) {
63             Log.e(LOG_TAG, "Remote Exception while writing value to device.");
64         }
65         }
66         else if(v.equals(clearButton)) {
67             String text = "";
68             valueText.setText(text);
69         }
70     }
71 }

The program obtains HelloService through ServiceManager.getService("hello"), and then converts it to the IHelloService interface through the IHelloService.Stub.asInterface function. Among them, the service name "hello" is specified when HelloService is loaded when the system starts, and the IHelloService interface is defined in android.os.IHelloService. For details, please refer to Adding hardware access services to the Application Frameworks layer of the Android system on Ubuntu one article. This program provides a simple function of reading the value of the custom hardware register val, which is implemented through the two interfaces of IHelloService.getVal and IHelloService.setVal.

Interface layout file res/layout/main.xml:
 1 <?xml version="1.0" encoding="utf-8"?>
 2     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3        android:orientation="vertical"
 4        android:layout_width="fill_parent"
 5        android:layout_height="fill_parent">
 6        <LinearLayout
 7           android:layout_width="fill_parent"
 8           android:layout_height="wrap_content"
 9           android:orientation="vertical" 
10           android:gravity="center">
11           <TextView 
12              android:layout_width="wrap_content"
13              android:layout_height="wrap_content" 
14              android:text="@string/value">
15           </TextView>
16           <EditText 
17              android:layout_width="fill_parent"
18              android:layout_height="wrap_content" 
19              android:id="@+id/edit_value"
20              android:hint="@string/hint">
21           </EditText>
22        </LinearLayout>
23        <LinearLayout
24           android:layout_width="fill_parent"
25           android:layout_height="wrap_content"
26           android:orientation="horizontal" 
27           android:gravity="center">
28           <Button 
29              android:id="@+id/button_read"
30              android:layout_width="wrap_content"
31              android:layout_height="wrap_content"
32              android:text="@string/read">
33           </Button>
34           <Button 
35              android:id="@+id/button_write"
36              android:layout_width="wrap_content"
37              android:layout_height="wrap_content"
38              android:text="@string/write">
39           </Button>
40           <Button 
41              android:id="@+id/button_clear"
42              android:layout_width="wrap_content"
43              android:layout_height="wrap_content"
44              android:text="@string/clear">
45           </Button>
46        </LinearLayout>
47     </LinearLayout>

String file res/values/strings.xml:

1 <?xml version="1.0" encoding="utf-8"?>  
2     <resources>  
3        <string name="app_name">Hello</string>  
4        <string name="value">Value</string>  
5        <string name="hint">Please input a value...</string>  
6        <string name="read">Read</string>  
7        <string name="write">Write</string>  
8        <string name="clear">Clear</string>  
9     </resources>  

Program description file AndroidManifest.xml:

 1 <?xml version="1.0" encoding="utf-8"?>  
 2     <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
 3       package="shy.luo.hello"  
 4       android:versionCode="1"  
 5       android:versionName="1.0">  
 6       <application android:icon="@drawable/icon" android:label="@string/app_name">  
 7         <activity android:name=".Hello"  
 8                   android:label="@string/app_name">  
 9             <intent-filter>  
10                 <action android:name="android.intent.action.MAIN" />  
11                 <category android:name="android.intent.category.LAUNCHER" />  
12             </intent-filter>  
13         </activity>  
14       </application>  
15     </manifest>   
3. Copy the Hello directory to the packages/experimental directory and add the Android.mk file: USER-NAME@MACHINE-NAME:~/Android/packages/experimental$ vi Android.mk
The contents of the Android.mk file are as follows:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := Hello
include $(BUILD_PACKAGE)
4. Compile:
USER-NAME@MACHINE-NAME:~/Android$ mmm  packages/experimental/Hello
After the compilation is successful, you can see the Hello.apk file in the out/target/product/generic/system/app directory.
5. Repackage the system image file system.img:
USER-NAME@MACHINE-NAME:~/Android$ make snod
The repackaged system.img file has the Hello.apk file built in.
6. Run the Android emulator:
USER-NAME@MACHINE-NAME:~/Android$ emulator -kernel kernel/common/arch/arm/boot/zImage &
The Hello app can be seen in the Home Screen:
Open the Hello app:
Click the Read button to read the value of the hardware register val from HelloService; click the Clear button to clear the value of the text box; enter a value in the text box, and then click the Write button to write the value to the hardware register val, you can click the Read button again to verify that the value was written correctly.
So far, we have completely learned the whole process of adding hardware drivers in Android's Linux kernel space, adding hardware interfaces in Android's hardware abstraction layer, providing hardware services in Android's Application Frameworks layer, and calling hardware services in Android's application layer. I hope it can provide readers with getting started help in entering the Android system. To relearn the whole process, please refer to Android Hardware Abstraction Layer (HAL) overview and learning plan.

Tags: Java Linux Android Ubuntu programming language

Posted by LostNights on Tue, 12 Jul 2022 03:36:12 +0930