Test the hardware service at the application frameworks layer for the Android system with built-in Java applications on Ubuntu.

Source: Internet
Author: User

The purpose of adding hardware services to the Android system is to allow applications at the application layer to access hardware services through Java interfaces. Then, how does an 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, then you can use this service to obtain the hardware service.

1. Refer to the article about adding hardware access services to the Application Frameworks layer of the Android system on Ubuntu, define your hardware service helloservice on the Application Frameworks layer, and provide the ihelloservice interface to provide access services.

2. To facilitate development, we can use the android SDK In the IDE environment to develop Android applications. After development, the program source code is transplanted to the android source code project directory. It is convenient to use the android plug-in ADT of eclipse to create an android project. For details, refer to other materials on the Internet. The project name is hello. The following main example shows the main file:

The main program is src/shy/LUO/Hello. Java:

package shy.luo.hello;import shy.luo.hello.R;import android.app.Activity;import android.os.ServiceManager;import android.os.Bundle;import android.os.IHelloService;import android.os.RemoteException;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class Hello extends Activity implements OnClickListener {private final static String LOG_TAG = "shy.luo.renju.Hello";private IHelloService helloService = null;private EditText valueText = null;private Button readButton = null;private Button writeButton = null;private Button clearButton = null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);helloService = IHelloService.Stub.asInterface(ServiceManager.getService("hello"));                valueText = (EditText)findViewById(R.id.edit_value);        readButton = (Button)findViewById(R.id.button_read);        writeButton = (Button)findViewById(R.id.button_write);        clearButton = (Button)findViewById(R.id.button_clear);readButton.setOnClickListener(this);writeButton.setOnClickListener(this);clearButton.setOnClickListener(this);                Log.i(LOG_TAG, "Hello Activity Created");    }        @Override    public void onClick(View v) {    if(v.equals(readButton)) {try {    int val = helloService.getVal();    String text = String.valueOf(val);    valueText.setText(text);} catch (RemoteException e) {Log.e(LOG_TAG, "Remote Exception while reading value from device.");}    }    else if(v.equals(writeButton)) {try {    String text = valueText.getText().toString();    int val = Integer.parseInt(text);helloService.setVal(val);} catch (RemoteException e) {Log.e(LOG_TAG, "Remote Exception while writing value to device.");}    }    else if(v.equals(clearButton)) {    String text = "";    valueText.setText(text);    }    }}

The program obtains helloservice through servicemanager. getservice ("hello"), and then converts it to the ihelloservice interface through the ihelloservice. stub. asinterface function. The service name "hello" is specified when the system loads helloservice at startup, while the ihelloservice interface is defined in Android. OS. for more information about ihelloservice, see add hardware access service to the Application Frameworks layer of the Android system on Ubuntu. This program provides a simple function to read custom hardware with the Val value of the Register, which is implemented through the ihelloservice. getval and ihelloservice. setval interfaces.

Interface layout file Res/layout/Main. xml:
<?xml version="1.0" encoding="utf-8"?>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       android:orientation="vertical"       android:layout_width="fill_parent"       android:layout_height="fill_parent">       <LinearLayout          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:orientation="vertical"           android:gravity="center">          <TextView              android:layout_width="wrap_content"             android:layout_height="wrap_content"              android:text="@string/value">          </TextView>          <EditText              android:layout_width="fill_parent"             android:layout_height="wrap_content"              android:id="@+id/edit_value"             android:hint="@string/hint">          </EditText>       </LinearLayout>       <LinearLayout          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:orientation="horizontal"           android:gravity="center">          <Button              android:id="@+id/button_read"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/read">          </Button>          <Button              android:id="@+id/button_write"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/write">          </Button>          <Button              android:id="@+id/button_clear"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/clear">          </Button>       </LinearLayout>    </LinearLayout>
String file Res/values/strings. xml:
<?xml version="1.0" encoding="utf-8"?>    <resources>       <string name="app_name">Hello</string>       <string name="value">Value</string>       <string name="hint">Please input a value...</string>       <string name="read">Read</string>       <string name="write">Write</string>       <string name="clear">Clear</string>    </resources>
The program description file androidmanifest. xml:
<?xml version="1.0" encoding="utf-8"?>    <manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="shy.luo.hello"      android:versionCode="1"      android:versionName="1.0">      <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".Hello"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>      </application>    </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 content of the android. mk file is 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/HelloAfter compilation, you can view the hello.apk file in the out/target/product/generic/system/appdirectory.
5. repackage the system image file system. IMG: User-name @ machine-Name :~ /Android $ make Snod
The new system.img file contains the hello.apk file. 6. Run the android simulator: User-name @ machine-Name :~ /Android $ emulator-kernel/common/ARCH/ARM/boot/zimage &In the home screen, you can see the hello application: Open the hello application: Click the read button to read the Val value of the hardware register from helloservice; click the Clear button to clear the value of the text box; enter a value in the text box and 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 is correctly written. So far, we have learned how to add hardware drivers to the Linux kernel space of Android, add hardware interfaces to the hardware abstraction layer of Android, provide hardware services at the application frameworks layer of Android, and provide hardware services at the application layer of Android. the whole process of calling the hardware service has been completed, hope to help readers get started with the Android system. For details about the entire process, refer to the overview and learning plan of the hardware abstraction layer (HAL) of Android. Lao Luo's Sina Weibo: http://weibo.com/shengyangluo. welcome to the attention!

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.