新增LED设备--从上层到底层理解安卓架构之Service篇
扫描二维码
随时随地手机看文章
Android内有许多系统管理服务,如
窗口管理服务WindowManagerService
电源管理服务PowerManagerService
通知管理服务NotifacationManagerService
电池管理服务BatteryManagerService等等
这些Manager提供了很多对系统层的控制接口。并且他们是由SystemServer在开机后启动并管理的。自定义的服务加入后,也类似这些系统服务,开机会自动启动,并且也有同样的上层的访问接口。
添加一个自定义的系统服务,需要按照service的框架要求实现以下几步:
1、TestLedService服务
路径:frameworks/base/services/core/java/com/android/server/TestLedService.java
package com.android.server;import android.content.Context;import android.util.Slog;import android.app.ITestLedService;import android.content.BroadcastReceiver;import android.app.ActivityManager;import android.content.ComponentName;import android.content.Intent;import android.content.IntentFilter;import android.os.SystemProperties;import android.content.pm.PackageManager;import java.io.BufferedReader;import java.io.IOException;import java.io.DataOutputStream;import java.io.DataInputStream;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import java.util.Date;public class TestLedService extends ITestLedService.Stub {private final Context mContext;private boolean mFlag;private boolean mled2On = false;private int mFlashDelay = 1000;public TestLedService(Context context) {super();mContext = context;testledinit_native();mFlag = true;if(mFlag){new Thread(new led2Flash()).start();}}public void SetLed1(int on){setled1_native(on);}public void SetLed2(int delay){mFlashDelay = delay;if(!mFlag && mFlashDelay != 0){new Thread(new led2Flash()).start();}if(delay == 0){mFlag = false;}}class led2Flash implements Runnable {public void run(){while(true){Slog.i("TestLed_service", "led2 flash");if(mled2On){setled2_native(0);mled2On = false;}else{setled2_native(1);mled2On = true;}try{Thread.sleep(mFlashDelay);}catch(Exception e){e.printStackTrace();}if(!mFlag){break;}}}}private static native boolean testledinit_native();private static native void setled1_native(int val);private static native void setled2_native(int val);}
TestLedService实现了两个led的控制的函数,SetLed1设置led1开与关,setLed2设置led2的闪烁频率。分别调用了JNI的函数setled1_native和setled2_native。
2、TestLedManager.java
路径:frameworks/base/core/java/android/app/TestLedManager.java
package android.app;import android.app.ITestLedService;import android.util.Slog;import android.os.RemoteException;public class TestLedManager {private final ITestLedService mService;public TestLedManager(ITestLedService mService) {this.mService = mService;}public void SetLed1(int on){try {mService.SetLed1(on);} catch (RemoteException ex) {ex.printStackTrace();}}public void SetLed2(int delay){try {mService.SetLed2(delay);} catch (RemoteException ex) {ex.printStackTrace();}}}
Manager是APP和service通讯的桥梁,通过系统接口getSystemService,app获取到该service的Manager。而在TestLedManager中就是将service内实现的函数接口重新封装了一下。
3、ITestLedService.aidl
路径:frameworks/base/core/java/android/app/ITestLedService.aidl
package android.app;
// Declare any non-default types here with import statementsinterface ITestLedService {/*** Demonstrates some basic types that you can use as parameters* and return values in AIDL.*/void SetLed1(int on);void SetLed2(int delay);}
TestLedService和TestLedManager在不同的进程,所以要考虑到进程通讯的问题。Manager通过增加一个aidl文件来描述通讯接口。
4、aidl加入编译mk
路径:frameworks/base/Android.mk
LOCAL_SRC_FILES += \……\core/java/android/app/ITestLedService.aidl \
5、Context.java新增服务名
路径:frameworks/base/core/java/android/content/Context.java
public static final String TestLed_SERVICE="TestLed";
6、注册服务
路径:frameworks/base/core/java/android/app/SystemServiceRegistry.java
import android.app.TestLedManager;import android.app.ITestLedService;final class SystemServiceRegistry {……static {……registerService(Context.TestLed_SERVICE, TestLedManager.class,new CachedServiceFetcher<TestLedManager>() {public TestLedManager createService(ContextImpl ctx) {IBinder b = ServiceManager.getService(Context.TestLed_SERVICE);ITestLedService service = ITestLedService.Stub.asInterface(b);return new TestLedManager(service);}});
7、启动服务
路径:frameworks/base/services/java/com/android/server/SystemServer.java
private void startOtherServices() {……TestLedService TestLed = new TestLedService(context);ServiceManager.addService(Context.TestLed_SERVICE, TestLed);
8、seliunx相关
路径:system/sepolicy/service.te
添加
type TestLed_service, system_api_service, system_server_service, service_manager_type;
路径:system/sepolicy/service_contexts
添加
TestLed u:object_r:TestLed_service:s0
编译过程中会报错,You have tried to change the API from what has been previously approved.是因为新增了系统api函数,make update -api 后再次编译即可。
烧录进设备并启动后,adb shell service list | grep TestLed *,就可看到新增的service已经在运行了,就说明成功了。
扫码关注我们
看更多嵌入式案例
喜欢本篇内容请给我们点个再看
免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!





