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

2016年(1)

2015年(4)

我的朋友

分类: LINUX

2015-07-20 17:43:10

一. 编写JNI方法提供Java访问硬件服务接口一文所示,为硬件抽象层模块准备好JNI方法调用层。
二. 在Android系统中,硬件服务一般是运行在一个独立的进程中为各种应用程序提供服务。因此,调用这些硬件服务的应用程序与这些硬件服务之间的通信需要 通过代理来进行。为此,我们要先定义好通信接口。进入到frameworks/base/core/java/android/os目录,新增 IHelloService.aidl接口定义文件,IHelloService.aidl定义了IHelloService接口:
  1. 1. package android.os;
  2. 2.
  3. 3. interface IHelloService {
  4. 4. void setVal(int val);
  5. 5. int getVal();
  6. 6. }
IHelloService接口主要提供了设备和获取硬件寄存器val的值的功能,分别通过setVal和getVal两个函数来实现。

三.返回到frameworks/base目录,打开Android.mk文件,修改LOCAL_SRC_FILES变量的值,增加IHelloService.aidl源文件:


  1. ## READ ME: ########################################################

  2. ##

  3. ## When updating this list of aidl files, consider if that aidl is

  4. ## part of the SDK API. If it is, also add it to the list below that

  5. ## is preprocessed and distributed with the SDK. This list should

  6. ## not contain any aidl files for parcelables, but the one below should

  7. ## if you intend for 3rd parties to be able to send those objects

  8. ## across process boundaries.

  9. ##

  10. ## READ ME: ########################################################

  11. LOCAL_SRC_FILES += /

  12. ....................................................................

  13. core/java/android/os/IVibratorService.aidl /

  14. core/java/android/os/IHelloService.aidl /

  15. core/java/android/service/urlrenderer/IUrlRendererService.aidl /

  16. .....................................................................
四. 编译IHelloService.aidl接口:
    USER-NAME@MACHINE-NAME:~/Android$ mmm frameworks/base

   这样,就会根据IHelloService.aidl生成相应的IHelloService.Stub接口。

   五.进入到frameworks/base/services/java/com/android/server目录,新增HelloService.java文件:

  1. 1. package com.android.server;
  2. 2. import android.content.Context;
  3. 3. import android.os.IHelloService;
  4. 4. import android.util.Slog;
  5. 5. public class HelloService extends IHelloService.Stub {
  6. 6. private static final String TAG = "HelloService";
  7. 7. HelloService() {
  8. 8. init_native();
  9. 9. }
  10. 10. public void setVal(int val) {
  11. 11. setVal_native(val);
  12. 12. }
  13. 13. public int getVal() {
  14. 14. return getVal_native();
  15. 15. }
  16. 16.
  17. 17. private static native boolean init_native();
  18. 18. private static native void setVal_native(int val);
  19. 19. private static native int getVal_native();
  20. 20. };
HelloService主要是通过调用JNI方法init_native、setVal_native和getVal_native来提供硬件服务。

六. 修改同目录的SystemServer.java文件,在ServerThread::run函数中增加加载HelloService的代码:

  1. @Override

  2. public void run() {

  3. ....................................................................................

  4. try {

  5. Slog.i(TAG, "DiskStats Service");

  6. ServiceManager.addService("diskstats", new DiskStatsService(context));

  7. } catch (Throwable e) {

  8. Slog.e(TAG, "Failure starting DiskStats Service", e);

  9. }

  10. try {

  11. Slog.i(TAG, "Hello Service");

  12. ServiceManager.addService("hello", new HelloService());

  13. } catch (Throwable e) {

  14. Slog.e(TAG, "Failure starting Hello Service", e);

  15. }

  16. ......................................................................................

  17. }
七. 编译HelloService和重新打包system.img:

   ~/Android$ mmm frameworks/base/services/java

  ~/Android$ make snod

     这样,重新打包后的system.img系统镜像文件就在Application Frameworks层中包含了我们自定义的硬件服务HelloService了,并且会在系统启动的时候,自动加载HelloService。这时,应 用程序就可以通过Java接口来访问Hello硬件服务了。我们将 在下一篇文章中描述如何编写一个Java应用程序来调用这个HelloService接口来访问硬件,敬请期待

转载:http://blog.csdn.net/luoshengyang/article/details/6578352
阅读(1379) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~