887
技術社區[雲棲]
在Ubuntu上為Android係統內置Java應用程序測試Application Frameworks層的硬件服務
我們在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); } } }
程序通過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和字符串文件res/values/strings.xml及程序描述文件AndroidManifest.xml參考下麵鏈接。
https://blog.csdn.net/yzhang8703/article/details/7163409
USER-NAME@MACHINE-NAME:~/Android/packages/experimental$ vi Android.mk
五. 重新打包係統鏡像文件system.img:
重新打包後的system.img文件就內置了Hello.apk文件了。

最後更新:2017-04-03 12:54:58
上一篇:
在Ubuntu為Android硬件抽象層(HAL)模塊編寫JNI方法提供Java訪問硬件服務接口
下一篇:
在Ubuntu上為Android係統的Application Frameworks層增加硬件訪問服務
政府安全資訊精選 2017年第十三期 網信辦發布《互聯網新聞信息服務新技術新應用安全評估管理規定》;Facebook頒布新廣告政策,加強內容安全
配置Mysql用戶權限命令
H5響應式網站建設該如何規劃
動態創建script標簽實現跨域資源訪問的方法
第3章 對象基礎
阿裏雲啟動天池電力AI大賽
互聯網醫療投資最需警惕“熱錢” 流量“質”比“量”靠譜
Qt的內存管理
[ASP.NET] 如果將緩存“滑動過期時間”設置為1秒會怎樣?
java 後台框架 支持APP接口調用 APP後台 java springmvc mybaits mysql oracle html5 後台框架 手機後台框架