In Ubuntu, implement the built-in Java application Test application frameworks layer hardware services for the Android system _android

Source: Internet
Author: User
Tags stub home screen

The purpose of adding hardware services to the Android system is to enable app-tier apps to access hardware services through the Java interface. So how does app access the hardware services provided by the application frameworks layer through the Java interface? In this article, we'll add a built-in application to the Android application layer, which uses the ServiceManager interface to get the specified service and then get the hardware service through the service.

I. Reference to Add hardware access to the application frameworks layer in Ubuntu Android , define your hardware services at the application frameworks level he Lloservice and provides access services for Ihelloservice interfaces.

Two. To facilitate development, we can use the Android SDK in the IDE environment to develop Android applications.

After the development is completed, the program source code is ported to the Android source code engineering directory. It is convenient to use the Eclipse's Android plug-in ADT to create the Android project, and you can refer to other information on the web. The project name is Hello, and the main example below is the primary file:

The main program is Src/shy/luo/hello/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.Hel
	
	Lo ";

	Private Ihelloservice helloservice = null;
	Private EditText valueText = null;
	Private Button Readbutton = null;
	Private Button Writebutton = null;
	
 Private Button Clearbutton = null; /** called the activity is a.
 * * @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 the HelloService through Servicemanager.getservice ("Hello"), and then converts the IHelloService.Stub.asInterface function to the Ihelloservice interface. Where the service name "Hello" is specified when the HelloService is loaded when the system is started. While the Ihelloservice interface is defined in Android.os.IHelloService, you can refer to the article on Ubuntu for adding hardware access to the application frameworks layer of the Android system. This program provides a simple read-custom hardware feature with register Val value, implemented through Ihelloservice.getval and ihelloservice.setval two 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" Wrap_content "android:text=" @string/value "> </TextView> <edittext android:layout_width=" Fill_parent "an
 droid: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=" W Rap_content "android:layout_height=" wrap_content "android:text=" @string/write "> </Button> <button Andro Id: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>

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>
 

three. Copy the Hello directory to the Packages/experimental directory, add android.mk files:

user-name@machine-name:~/android/packages/experimental$ VI android.mk

The contents of the ANDROID.MK document 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)

Four. 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.

Five. Repackaging system image files System.img:

user-name@machine-name:~/android$ make Snod

The hello.apk file is built into the repackaged system.img file.

Six. Run the Android simulator:

user-name@machine-name:~/android$ Emulator-kernel Kernel/common/arch/arm/boot/zimage &

You can see the Hello Application in Home screen:

Open the Hello application:

Click the Read button to read the value of the hardware register Val from the HelloService, click the Clear button to empty the value of the text box, enter a number 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 is correctly written.
At this point, we've fully learned to add hardware drivers to Android's Linux kernel space, add hardware interfaces to the Android hardware abstraction layer, and application on Android The frameworks layer provides hardware services and the entire process of invoking hardware services at the Android application layer, hoping to provide introductory help for readers to enter the Android system. Learn the whole process again.

Series of articles:

Android source downloaded, compiled and installed in Ubuntu

The Android kernel source is downloaded, compiled, and installed in Ubuntu

How to write Linux kernel driver implementation on Ubuntu Android system

Ubuntu Android Hal Writes JNI method provides Java Access hardware service interface

Ubuntu Android implements application frameworks layer to add Hardware access services

The above is the simple application of Ubuntu Android, from the kernel to the drive, the Hal,frameworks layer, in the implementation of the app, hoping to help deep research on the Android source friends.

Related Article

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.