内容简介:做系统开发,有时候需要自己定义一些接口供App使用, 同时为了方便维护管理,就会需要自己建立一个服务,把新的功能集中在一起。下面就是新建一个系统服务的基本步骤。
做系统开发,有时候需要自己定义一些接口供App使用, 同时为了方便维护管理,就会需要自己建立一个服务,把新的功能集中在一起。下面就是新建一个系统服务的基本步骤。
-
添加接口
frameworks/base/core/java/android/app/IDemoManager.aidl
package android.app; interface IDemoManager { int getCpuTemperature(); }
-
添加服务,实现aidl文件定义的接口
frameworks/base/services/core/java/com/android/server/DemoManagerService.java
package com.android.server; import android.app.IDemoManager; import android.content.Context; import android.util.Slog; public class DemoManagerService extends IDemoManager.Stub { private Context mContext; public DemoManagerService(Context context) { mContext = context; Slog.d("Demo", "Construct"); } @Override public int getCpuTemperature() { return 100; // Test code } }
-
添加对应的Manager
frameworks/base/core/java/android/app/DemoManager.java
package android.app; import android.content.Context; import android.os.RemoteException; import android.util.Slog; public class DemoManager { Context mContext; IDemoManager mService; public DemoManager(Context context, IDemoManager service) { mContext = context; mService = service; } public int getCpuTemperature() { if (mService != null) { try { return mService.getCpuTemperature(); } catch (RemoteException e) { Slog.e("Demo", "RemoteException " + e); } } return -1; } }
-
添加aidl到Makefile src
frameworks/base/Android.mk
LOCAL_SRC_FILES += \ core/java/android/app/IDemoManager.aidl \
-
添加DEMO_SERVICE常量
frameworks/base/core/java/android/content/Context.java
public static final String DEMO_SERVICE = "demo";
-
注册系统服务
frameworks/base/core/java/android/app/SystemServiceRegistry.java
registerService(Context.ORISLINK_SERVICE, DemoManager.class, new CachedServiceFetcher<DemoManager>() { @Override public DemoManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.DEMO_SERVICE); return new DemoManager(ctx, IDemoManager.Stub.asInterface(b)); }});
-
开机启动服务
frameworks/base/services/java/com/android/server/SystemServer.java
try { ServiceManager.addService(Context.DEMO_SERVICE, new DemoManagerService(context)); } catch (Throwable e) { Slog.e("Demo", "Failed to start Demo Service " + e); }
-
编译源码,因为添加了接口,所以需要
make update-api
更新接口。然后再整编刷机。
-
service list 查看服务,不存在,这是因为selinux权限没加。
-
添加sepolicy权限
device/qcom/sepolicy/msm8937/service.te
type demo_service, system_api_service, system_server_service, service_manager_type;
device/qcom/sepolicy/msm8937/service_contexts
demo u:object_r:demo_service:s0
-
重新编译代码,使用下面测试代码验证
import android.app.DemoManager; DemoManager om = (DemoManager) getSystemService(Context.DEMO_SERVICE); Log.d(TAG, "Current temperature is " + om.getCpuTemperature());
最终log打印出100,服务添加完成。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Linux内核编译及添加系统调用
- CentOS 7 将 Nginx 添加系统服务
- 码良更新,添加更直观的样式编辑系统
- [Framework] 在Android rom添加系统jar包
- 微软对 Chromium 新贡献:为字幕添加 Windows 系统样式支持
- 为基于spring-boot的应用添加根据运行时操作系统环境来提示用户选择active profile的功能
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Book of CSS3
Peter Gasston / No Starch Press / 2011-5-13 / USD 34.95
CSS3 is the technology behind most of the eye-catching visuals on the Web today, but the official documentation can be dry and hard to follow. Luckily, The Book of CSS3 distills the heady technical la......一起来看看 《The Book of CSS3》 这本书的介绍吧!