在上一篇文章中,我們看到了如果在java程序中調用C/C++撰寫的函數。而且Android的service已經實現了,下面就要看看應用程序如何調用這個service了,這裡用兩種方法,我們先介紹簡單的第一種直接調用方法。
apps/mokoid/apps/LedClient/src/com/mokoid/LedClient/LedClient.java
- package com.mokoid.LedClient;
- import com.mokoid.server.LedService;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class LedClient extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // Call an API on the library.
- LedService ls = new LedService();
- ls.setOn(1);
- ls.setOff(2);
- TextView tv = new TextView(this);
- tv.setText("LED 1 is on. LED 2 is off.");
- setContentView(tv);
- }
- }
上面的代碼很簡單,直接調用上一篇文章介紹的LedService,然後利用這個service的成員函數setOn和setOff直接操作硬件(通過jni->HAL)。