Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20967
  • 博文数量: 5
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-22 11:53
文章分类
文章存档

2016年(1)

2015年(4)

我的朋友

分类: LINUX

2015-07-20 17:43:22

       我们在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务。那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务呢?在这一篇文章中,我们将在Android系统的应用层增加一个内置的应用程序,这个内置的应用程序通过 ServiceManager接口获取指定的服务,然后通过这个服务来获得硬件服务。
一. 在Application Frameworks层定义好自己的硬件服务HelloService,并提供IHelloService接口提供访问服务。
二. 为了方便开发,我们可以在IDE环境下使用Android SDK来开发Android应用程序。开发完成后,再把程序源代码移植到Android源代码工程目录中。使用Eclipse的Android插件ADT 创建Android工程很方便,这里不述,可以参考网上其它资料。工程名称为Hello,下面主例出主要文件:

    主程序是src/shy/luo/hello/Hello.java:

  1. 1. package shy.luo.hello;
  2. 2.
  3. 3. import shy.luo.hello.R;
  4. 4. import android.app.Activity;
  5. 5. import android.os.ServiceManager;
  6. 6. import android.os.Bundle;
  7. 7. import android.os.IHelloService;
  8. 8. import android.os.RemoteException;
  9. 9. import android.util.Log;
  10. 10. import android.view.View;
  11. 11. import android.view.View.OnClickListener;
  12. 12. import android.widget.Button;
  13. 13. import android.widget.EditText;
  14. 14.
  15. 15. public class Hello extends Activity implements OnClickListener {
  16. 16. private final static String LOG_TAG = "shy.luo.renju.Hello";
  17. 17.
  18. 18. private IHelloService helloService = null;
  19. 19.
  20. 20. private EditText valueText = null;
  21. 21. private Button readButton = null;
  22. 22. private Button writeButton = null;
  23. 23. private Button clearButton = null;
  24. 24.
  25. 25. /** Called when the activity is first created. */
  26. 26. @Override
  27. 27. public void onCreate(Bundle savedInstanceState) {
  28. 28. super.onCreate(savedInstanceState);
  29. 29. setContentView(R.layout.main);
  30. 30.
  31. 31. helloService = IHelloService.Stub.asInterface(
  32. 32. ServiceManager.getService("hello"));
  33. 33.
  34. 34. valueText = (EditText)findViewById(R.id.edit_value);
  35. 35. readButton = (Button)findViewById(R.id.button_read);
  36. 36. writeButton = (Button)findViewById(R.id.button_write);
  37. 37. clearButton = (Button)findViewById(R.id.button_clear);
  38. 38.
  39. 39. readButton.setOnClickListener(this);
  40. 40. writeButton.setOnClickListener(this);
  41. 41. clearButton.setOnClickListener(this);
  42. 42.
  43. 43. Log.i(LOG_TAG, "Hello Activity Created");
  44. 44. }
  45. 45.
  46. 46. @Override
  47. 47. public void onClick(View v) {
  48. 48. if(v.equals(readButton)) {
  49. 49. try {
  50. 50. int val = helloService.getVal();
  51. 51. String text = String.valueOf(val);
  52. 52. valueText.setText(text);
  53. 53. } catch (RemoteException e) {
  54. 54. Log.e(LOG_TAG, "Remote Exception while reading value from device.");
  55. 55. }
  56. 56. }
  57. 57. else if(v.equals(writeButton)) {
  58. 58. try {
  59. 59. String text = valueText.getText().toString();
  60. 60. int val = Integer.parseInt(text);
  61. 61. helloService.setVal(val);
  62. 62. } catch (RemoteException e) {
  63. 63. Log.e(LOG_TAG, "Remote Exception while writing value to device.");
  64. 64. }
  65. 65. }
  66. 66. else if(v.equals(clearButton)) {
  67. 67. String text = "";
  68. 68. valueText.setText(text);
  69. 69. }
  70. 70. }
  71. 71. }
 程序通过ServiceManager.getService("hello")来获得HelloService,接着通过 IHelloService.Stub.asInterface函数转换为IHelloService接口。其中,服务名字“hello”是系统启动时加 载HelloService时指定的,而IHelloService接口定义在android.os.IHelloService中。这个程序提供了简单的读定自定义硬件有寄存器val的值的功能,通过IHelloService.getVal和IHelloService.setVal两个接口实现。

界面布局文件res/layout/main.xml:
  1. 1.
  2. 2.
  3. 3. android:orientation="vertical"
  4. 4. android:layout_width="fill_parent"
  5. 5. android:layout_height="fill_parent">
  6. 6.
  7. 7. android:layout_width="fill_parent"
  8. 8. android:layout_height="wrap_content"
  9. 9. android:orientation="vertical"
  10. 10. android:gravity="center">
  11. 11.
  12. 12. android:layout_width="wrap_content"
  13. 13. android:layout_height="wrap_content"
  14. 14. android:text="@string/value">
  15. 15.
  16. 16.
  17. 17. android:layout_width="fill_parent"
  18. 18. android:layout_height="wrap_content"
  19. 19. android:id="@+id/edit_value"
  20. 20. android:hint="@string/hint">
  21. 21.
  22. 22.
  23. 23.
  24. 24. android:layout_width="fill_parent"
  25. 25. android:layout_height="wrap_content"
  26. 26. android:orientation="horizontal"
  27. 27. android:gravity="center">
  28. 28.
  29. 34.
  30. 40.
  31. 46.
  32. 47.

字符串文件res/values/strings.xml:

  1. 1.
  2. 2.
  3. 3. Hello
  4. 4. Value
  5. 5. Please input a value...
  6. 6. Read
  7. 7. Write
  8. 8. Clear
  9. 9.
程序描述文件AndroidManifest.xml:
  1. #
  2. #
  3. # package="shy.luo.hello"
  4. # android:versionCode="1"
  5. # android:versionName="1.0">
  6. #
  7. #
  8. # android:label="@string/app_name">
  9. #
  10. #
  11. #
  12. #
  13. #
  14. #
  15. #
  16. # android:minSdkVersion="7"
  17. # android:targetSdkVersion="7">
  18. #
  19. #
三. 将Hello目录拷贝至packages/experimental目录,新增Android.mk文件:
   ~/Android/packages/experimental$ vi Android.mk Android.mk的文件内容如下:
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)
四. 编译:
~/Android$ mmm packages/experimental/Hello
编译成功后,便可以在out/target/product/generic/system/app目录下看到Hello.apk文件了。
五. 重新打包系统镜像文件system.img:
~/Android$ make snod
    重新打包后的system.img文件就内置了Hello.apk文件了。
六. 运行Android模拟器:
~/Android$ emulator -kernel kernel/common/arch/arm/boot/zImage &
在Home Screen中可以看到Hello应用程序:


打开Hello应用程序:

点击Read按钮,可以从HelloService中读取硬件寄存器val的值;点击Clear按钮,可以清空文本框的值;在文本框中输入一个数值,再点击Write按钮,便可以将这个值写入到硬件寄存器val中去,可以再次点击Read按钮来验证是否正确写入了值。
至 此,我们就完整地学习了在Android的Linux内核空间添加硬件驱动程序、在Android的硬件抽象层添加硬件接口、在Android的 Application Frameworks层提供硬件服务以及在Android的应用层调用硬件服务的整个过程了,希望能为读者进入Android系统提供入门帮助。

转载:http://blog.csdn.net/luoshengyang/article/details/6580267



阅读(1402) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~