添加service到SystemService硬件服务
修改时间:2015年3月10日(星期二) 中午11:37
.添加service到SystemService
IInnerToolService 定义接口,InnerToolService实现该接口
IInnerToolCB.aidl 定义回调 不熟悉aidl的话可以先不加
1).添加需要的Service的java文件。
注意:包名也要一样。package com.android.server;然后找到SystemServer,修改如下在ServerThread的run方法里添加:
//add innertool
try {
ServiceManager.addService("innertool", new InnerToolService(context));
} catch (Throwable e) {
}
//End
注意"innertool"相当于service的标记,在使用getSystemService来获取时需要提供该标记。
2).frameworks\base\core\java\android\app\ContextImpl.java
这里主要是响应getSystemService,如果service未启动,则启动service。(与manager关联)
首先添加import语句
//inner tool
import com.android.innertool.IInnerToolService;
//End 然后在内部类ServiceFetcher的static语句块里添加以下代码: //add innertool
registerService("innertool", new ServiceFetcher() {
public Object getService(ContextImpl ctx) {
IBinder b = ServiceManager.getService("innertool");
IInnerToolService service = IInnerToolService.Stub.asInterface(b);
Log.e("tool", "fetch innertool service = " + service);
return service;
}});
//End
其次在context/context.java中添加对应函数
3).aidl相关文件的编译
framework/base/Android.mk文件,在LOCAL_SRC_FILES下添加相关的aidl文件
LOCAL_SRC_FILES += \
innertool/java/com/android/innertool/IInnerToolService.aidl \
innertool/java/com/android/innertool/IInnerToolCB.aidl
到这里基本就添加完成了,可能部分不同的平台或者项目会存在差异,大家需要细心。然后是对于远程service的调用
假设aidl定义的接口如下
interface IInnerToolService{
void exec(String arg0,String arg1,IInnerToolCB arg2);
}
2.通过aidl调用远程service
上面定义了aidl文件,使用的包名是com.android.innertool,那我们本地应用也需要有这个包名下的aidl文件
即测试应用需要创建com.android.innertool这个包,并将IInnerToolService.aidl和IInnerToolCB.aidl文件放进去,这样会在gen/生成对应的java文件。
然后就可以使用该SystemService的接口了。
private IInnerToolService toolService2;
toolService2 = (IInnerToolService) getSystemService("innertool");
try {
toolService2.exec("aaa", "bbb", new InnerToolCB());
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
附上相应的代码
package com.android.server; import android.content.Context;
import android.os.RemoteException;
import android.util.Log; import com.android.innertool.IInnerToolCB;
import com.android.innertool.IInnerToolService; public class InnerToolService extends IInnerToolService.Stub{ private Context mContext; public InnerToolService(Context context) {
mContext = context;
} @Override
public void exec(String arg0, String arg1, IInnerToolCB arg2)
throws RemoteException {
// TODO Auto-generated method stub
Log.v("InnerToolService", "exec,"+arg0+","+arg1+","+arg2);
}
}
==========================================
注意: 如果在Android源码中添加了自定义的包、类、方法或者你修改了android源码中标识为“@hide”的方法、类,你需要这些内容对Application可见并且需要编译进sdk中的Document中的话,需要执行
make update-api
==========================================
添加service到SystemService硬件服务的更多相关文章
- 问题:Custom tool error: Failed to generate code for the service reference 'AppVot;结果:添加Service Reference, 无法为服务生成代码错误的解决办法
添加Service Reference, 无法为服务生成代码错误的解决办法 我的解决方案是Silverlight+WCF的应用,Done Cretiria定义了需要在做完Service端的代码后首先运 ...
- linux 添加 service 服务并自动添加 chkconfig 启动级别
下面以添加一个叫做watchcat的服务为例进行说明: 1.写一个提供给service命令使用的脚本 service 命令的使用方法一般如下 启动: $ service watchcat start ...
- Android 从硬件到应用程序:一步一步爬上去 5 -- 在Frameworks蒂姆层硬件服务
Android Frameworks层提供硬件服务,Android系统APP能够调用这些硬件服务,而硬件则完全控制.实现应有的功能.上一页下一页.为了这一个frameworks高层的应用java接口硬 ...
- LBS(Location Based Service)(基于位置的服务)
LBS(Location Based Service)(基于位置的服务) Android 中定位方式基本可以分为两种:GPS定位,网络定位. GPS定位的工作原理是基于手机内置的GPS硬件直接和卫星进 ...
- Tiny4412 LED 硬件服务
1.Android系统中启动框架 2.首先实现驱动程序 #include <linux/kernel.h> #include <linux/module.h> #include ...
- 转:无法向会话状态服务器发出会话状态请求请。确保 ASP.NET State Service (ASP.NET 状态服务)已启动
今天看到一篇文章感觉不错,收藏转载下. 原文地址:http://blog.csdn.net/sntyy/article/details/2090347 版权为原作者所有 无法向会话状态服务器发出会话状 ...
- Android Service学习之本地服务
Service是在一段不定的时间运行在后台,不和用户交互应用组件.每个Service必须在manifest中 通过来声明.可以通过contect.startservice和contect.bindse ...
- 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(老罗学习笔记6)
一:Eclipse下 1.创建工程: ---- 2.创建后目录 3.添加java函数 4.在src下创建package,在package下创建file 5.res---layout下创建xml文件,命 ...
- 为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
我们在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务.那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务 ...
随机推荐
- 十分钟搭建个人网站:Jekyll主题BoHu
最近花了三天时间制作了我的第一个jekyll theme--BoHu.一款知乎风格的模板,使用jekyll模板引擎,十分钟就能搭建属于你自己的静态博客网站. 本主题的特征为: 知乎风格 分页导航使用的 ...
- 7zS.sfx RunProgram with parameters
Config.txt as below: Pay attention to this \" ;!@Install@!UTF-8! RunProgram="setup.exe&qu ...
- C++ DLL 获取 MSI Property
VS2010 创建 C++, Win32 DLL工程C-TEST. Stdafx.h中,在<windows.h>之后 添加引用. #include <msi.h> #incl ...
- 在Windows下用Mingw 4.5.2编译X264
1.下载mingw-get-inst-20110530.rar(http://www.baidu.com/link?url=-ixXW6QiuEl8CA1dKudoWCxzcTvxrpQ0nXRBHU ...
- Windows server2008/2012 安装oracle 11 创建实例HANG住在百分之2
Windows server2008/2012 安装oracle 11.2.0.1的时候,可能会在创建数据库实例的时候卡在百分之2的地方. 这个时候可以 1.点击开始菜单,在“搜索程序和文件”中输入“ ...
- 在JQuery和Js中,如何让ajax执行完后再继续往下执行 async
async (默认: true) 默认设置下,所有请求均为异步请求.如果需要发送同步请求,请将此选项设置为 false.注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行. var t ...
- 让hyper-v调整console的大小
在hyper-v中centos的console一直都是1024x768的分辨率,后来找到一种修改分辨率的解决方法 grubby --update-kernel=ALL --args="vid ...
- lamp 中基本配置常识
// apache// 禁止访问目录// 开启 url重写// 重写定义错误页面// 日志分页// 增加并发连接数// 设置连接连接的时间 // threadsPerChild // 每个进程的线程数 ...
- Yii 配置默认controller和action
设置默认controller 在/protected/config/main.php添加配置 <?php return array( 'name'=>'Auto', 'defaultCon ...
- 【5】了解Bootstrap预置的栅格系统
在开篇之前我们来说2个class,因为以后要用到的 <div class="container"> ... </div> 用.container包裹页面上的 ...