为Android系统内置Java应用程序测试Application Frameworks层的硬件服务

我们在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务。那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务呢?在这一篇文章中,我们将在Android系统的应用层增加一个内置的应用程序,这个内置的应用程序通过ServiceManager接口获取指定的服务,然后通过这个服务来获得硬件服务。

 一. 在Application Frameworks层定义好自己的硬件服务HelloService,并提供IHelloService接口提供访问服务。

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

  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中。这个程序提供了简单的读定自定义硬件有寄存器val的值的功能,通过IHelloService.getVal和IHelloService.setVal两个接口实现。



后面的测试,android.mk比较简单就不说了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值