博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
添加service到SystemService硬件服务
阅读量:6966 次
发布时间:2019-06-27

本文共 3040 字,大约阅读时间需要 10 分钟。

添加service到SystemService: 添加硬件服务。
创建时间:2015年3月9日(星期一) 晚上11:07 | 分类:硬件驱动Android | 天气: 
修改时间:2015年3月10日(星期二) 中午11:37
<note_content>

.添加service到SystemService

测试的
service类是InnerToolService,表示内置到framework的自定义服务,使用的aidl包括:
IInnerToolService  定义接口,InnerToolService实现该接口
IInnerToolCB.aidl  定义回调  不熟悉aidl的话可以先不加
 
1).添加需要的Service的java文件。

注意:包名也要一样。package com.android.server;然后找到SystemServer,修改如下在ServerThread的run方法里添加

//add innertooltry {                  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
  1. 其次在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
==========================================

转载地址:http://unisl.baihongyu.com/

你可能感兴趣的文章
JavaScript_BOM_window
查看>>
Hadoop:The Definitive Guid 总结 Chapter 7 MapReduce的类型与格式
查看>>
WCF 入门之旅(4): 怎样用客户端调用WCF服务
查看>>
oracle12之 多租户容器数据库架构
查看>>
POJ3061 ZOJ3123 Subsequence【前缀和+二分搜索+尺取法】
查看>>
png库结合zlib库使用出现的一个链接问题的解决
查看>>
Hibernate总结(二)
查看>>
TSP问题
查看>>
ubuntu14.06 Lts开启ssh服务
查看>>
对象比较:Comparable 和 Comparator
查看>>
jsp中的contentType与pageEncoding的区别和作用
查看>>
java 调用启动远程shell脚本,启动spark
查看>>
Spring boot ----RestTemplate学习笔记
查看>>
[LUOGU] P3128 [USACO15DEC]最大流Max Flow
查看>>
windows2003server下能安装的MSN
查看>>
Caffe将自己的文件生成lmdb
查看>>
C# 枚举中的位运算
查看>>
Codeforces Global Round 1 晕阙记
查看>>
百度文化秘籍
查看>>
Algs4-1.3.33一个双向队列Deque-双向链表实现
查看>>