Android之测试Jave接口访问硬件服务

2023-10-31 21:32

本文主要是介绍Android之测试Jave接口访问硬件服务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我们在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:

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

界面布局文件res/layout/main.xml:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.        android:orientation="vertical"  
  4.        android:layout_width="fill_parent"  
  5.        android:layout_height="fill_parent">  
  6.        <LinearLayout  
  7.           android:layout_width="fill_parent"  
  8.           android:layout_height="wrap_content"  
  9.           android:orientation="vertical"   
  10.           android:gravity="center">  
  11.           <TextView   
  12.              android:layout_width="wrap_content"  
  13.              android:layout_height="wrap_content"   
  14.              android:text="@string/value">  
  15.           </TextView>  
  16.           <EditText   
  17.              android:layout_width="fill_parent"  
  18.              android:layout_height="wrap_content"   
  19.              android:id="@+id/edit_value"  
  20.              android:hint="@string/hint">  
  21.           </EditText>  
  22.        </LinearLayout>  
  23.        <LinearLayout  
  24.           android:layout_width="fill_parent"  
  25.           android:layout_height="wrap_content"  
  26.           android:orientation="horizontal"   
  27.           android:gravity="center">  
  28.           <Button   
  29.              android:id="@+id/button_read"  
  30.              android:layout_width="wrap_content"  
  31.              android:layout_height="wrap_content"  
  32.              android:text="@string/read">  
  33.           </Button>  
  34.           <Button   
  35.              android:id="@+id/button_write"  
  36.              android:layout_width="wrap_content"  
  37.              android:layout_height="wrap_content"  
  38.              android:text="@string/write">  
  39.           </Button>  
  40.           <Button   
  41.              android:id="@+id/button_clear"  
  42.              android:layout_width="wrap_content"  
  43.              android:layout_height="wrap_content"  
  44.              android:text="@string/clear">  
  45.           </Button>  
  46.        </LinearLayout>  
  47.     </LinearLayout>  
字符串文件res/values/strings.xml:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <resources>  
  3.        <string name="app_name">Hello</string>  
  4.        <string name="value">Value</string>  
  5.        <string name="hint">Please input a value...</string>  
  6.        <string name="read">Read</string>  
  7.        <string name="write">Write</string>  
  8.        <string name="clear">Clear</string>  
  9.     </resources>  
程序描述文件AndroidManifest.xml:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="shy.luo.hello"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.       <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".Hello"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.       </application>  
  15.     </manifest>   
三. 将Hello目录拷贝至packages/experimental目录,新增Android.mk文件:
     USER-NAME@MACHINE-NAME:~/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)
四. 编译:
USER-NAME@MACHINE-NAME:~/Android$ mmm  packages/experimental/Hello
编译成功后,便可以在out/target/product/generic/system/app目录下看到Hello.apk文件了。
    五. 重新打包系统镜像文件system.img:
USER-NAME@MACHINE-NAME:~/Android$ make snod
    重新打包后的system.img文件就内置了Hello.apk文件了。
六. 运行Android模拟器:
USER-NAME@MACHINE-NAME:~/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系统提供入门帮助。重新学习整个过程,请参考 Android硬件抽象层(HAL)概要介绍和学习计划。

这篇关于Android之测试Jave接口访问硬件服务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/318276

相关文章

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

TP-Link PDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务

《TP-LinkPDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务》近期,路由器制造巨头普联(TP-Link)在用户群体中引发了一系列重要变动,上个月,公司发出了一则通知,明确要求所... 路由器厂商普联(TP-Link)上个月发布公告要求所有用户必须完成实名认证后才能继续使用普联提供的 D

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec