序言:这两篇文章转自老罗的博客,讲解了aidl在framework层的使用,在前一篇文章中讲到了aidl可以用于android四大组件service中用于进程间的通讯;这里讲解了继承aidl生成的类来制作android servicemanager管理的service进行进程间的通讯;由此可见aidl不仅可以用于android的四大组件中的service同样可以用于framework层servicemanager管理的service,其实原理上是一样的,都是通过binder进行通讯。
在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行。今天大家对iOS和Android系统的趋之若鹜,一定程度上是由于这两个系统上有着丰富多彩的各种应用软件。因此,软件和硬件的关系,在一定程度上可以说,硬件是为软件服务的。硬件工程师研发出一款硬件设备,自然少了软件工程师为其编写驱动程序;而驱动程序的最终目的,是为了使得最上层的应用程序能够使用这些硬件提供的服务来为用户提供软件功能。对Android系统上的应用软件来说,就是要在系统的Application Frameworks层为其提供硬件服务。在前面的几篇文章中,我们着重介绍了Linux内核层、硬件抽象层和运行时库层提供的自定义硬件服务接口,这些接口都是通过C或者C++语言来实现的。在这一篇文章中,我们将介绍如何在Android系统的Application Frameworks层提供Java接口的硬件服务。
一. 参照在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口一文所示,为硬件抽象层模块准备好JNI方法调用层。
二. 在Android系统中,硬件服务一般是运行在一个独立的进程中为各种应用程序提供服务。因此,调用这些硬件服务的应用程序与这些硬件服务之间的通信需要通过代理来进行。为此,我们要先定义好通信接口。进入到frameworks/base/core/java/android/os目录,新增IHelloService.aidl接口定义文件:
USER-NAME@MACHINE-NAME:~/Android$ cd frameworks/base/core/java/android/os
USER-NAME@MACHINE-NAME:~/Android/frameworks/base/core/java/android/os$ vi IHelloService.aidl
IHelloService.aidl定义了IHelloService接口:
- package android.os;
- interface IHelloService {
- void setVal(int val);
- int getVal();
- }
IHelloService接口主要提供了设备和获取硬件寄存器val的值的功能,分别通过setVal和getVal两个函数来实现。
三.返回到frameworks/base目录,打开Android.mk文件,修改LOCAL_SRC_FILES变量的值,增加IHelloService.aidl源文件:
## 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/IVibratorService.aidl /
core/java/android/os/IHelloService.aidl /
core/java/android/service/urlrenderer/IUrlRendererService.aidl /
.....................................................................
这样,就会根据IHelloService.aidl生成相应的IHelloService.Stub接口。
五.进入到frameworks/base/services/java/com/android/server目录,新增HelloService.java文件:
- package com.android.server;
- import android.content.Context;
- import android.os.IHelloService;
- import android.util.Slog;
- public class HelloService extends IHelloService.Stub {
- 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主要是通过调用JNI方法init_native、setVal_native和getVal_native(见在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口一文)来提供硬件服务。
六. 修改同目录的SystemServer.java文件,在ServerThread::run函数中增加加载HelloService的代码:
@Override
public void run() {
....................................................................................
try {
Slog.i(TAG, "DiskStats Service");
ServiceManager.addService("diskstats", new DiskStatsService(context));
} catch (Throwable e) {
Slog.e(TAG, "Failure starting DiskStats Service", e);
}
try {
Slog.i(TAG, "Hello Service");
ServiceManager.addService("hello", new HelloService());
} catch (Throwable e) {
Slog.e(TAG, "Failure starting Hello Service", e);
}
......................................................................................
}
七. 编译HelloService和重新打包system.img:
USER-NAME@MACHINE-NAME:~/Android$ mmm frameworks/base/services/java
USER-NAME@MACHINE-NAME:~/Android$ make snod
这样,重新打包后的system.img系统镜像文件就在Application Frameworks层中包含了我们自定义的硬件服务HelloService了,并且会在系统启动的时候,自动加载HelloService。这时,应用程序就可以通过Java接口来访问Hello硬件服务了。我们将在下一篇文章中描述如何编写一个Java应用程序来调用这个HelloService接口来访问硬件,敬请期待。
老罗的新浪微博:http://weibo.com/shengyangluo,欢迎关注!
我们在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务。那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务呢?在这一篇文章中,我们将在Android系统的应用层增加一个内置的应用程序,这个内置的应用程序通过ServiceManager接口获取指定的服务,然后通过这个服务来获得硬件服务。
一. 参照在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务一文,在Application Frameworks层定义好自己的硬件服务HelloService,并提供IHelloService接口提供访问服务。
二. 为了方便开发,我们可以在IDE环境下使用Android SDK来开发Android应用程序。开发完成后,再把程序源代码移植到Android源代码工程目录中。使用Eclipse的Android插件ADT创建Android工程很方便,这里不述,可以参考网上其它资料。工程名称为Hello,下面主例出主要文件:
主程序是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.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);
- }
- }
- }
- <?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>
- <?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>
- <?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>
USER-NAME@MACHINE-NAME:~/Android/packages/experimental$ vi Android.mk
五. 重新打包系统镜像文件system.img:
重新打包后的system.img文件就内置了Hello.apk文件了。