現在的位置: 首頁 > 綜合 > 正文

Android HAL 開發 (4)

2014年02月12日 ⁄ 綜合 ⁄ 共 817字 ⁄ 字號 評論關閉

在上一篇文章中,我們看到了如果在java程序中調用C/C++撰寫的函數。而且Android的service已經實現了,下面就要看看應用程序如何調用這個service了,這裡用兩種方法,我們先介紹簡單的第一種直接調用方法。

apps/mokoid/apps/LedClient/src/com/mokoid/LedClient/LedClient.java

  1. package com.mokoid.LedClient; 
  2. import com.mokoid.server.LedService; 
  3.  
  4. import android.app.Activity; 
  5. import android.os.Bundle; 
  6. import android.widget.TextView; 
  7.  
  8. public class LedClient extends Activity { 
  9.     @Override 
  10.     public void onCreate(Bundle savedInstanceState) { 
  11.         super.onCreate(savedInstanceState); 
  12.  
  13.         // Call an API on the library. 
  14.     LedService ls = new LedService(); 
  15.     ls.setOn(1); 
  16.     ls.setOff(2); 
  17.          
  18.         TextView tv = new TextView(this); 
  19.         tv.setText("LED 1 is on. LED 2 is off."); 
  20.         setContentView(tv); 
  21.     } 

上面的代碼很簡單,直接調用上一篇文章介紹的LedService,然後利用這個service的成員函數setOn和setOff直接操作硬件(通過jni->HAL)。

抱歉!評論已關閉.