由于系统api并没有给我们提供itelephony这个电话管理服务类的接口使用,所以我们就得通过非正常手段来得到这个服务接口.(通过源码中的itelephony.aidl来帮助我们生成电话管理服务接口,从而使我们能够使用到系统操作电话的功能).
例如>>结束通话:
1> 拷贝连同包结构将用到的itelephony.aidl文件到你的项目中
(由于其中引入了android.telephony.neighboringcellinfo.aidl, 所以将其也引入到你的项目中去)
要想挂断电话必须具有<uses-permission android:name="android.permission.call_phone"/>权限
try{
//反射获得系统服务的getservice方法对象
method method = class.forname("android.os.servicemanager")
.getmethod("getservice", string.class);
//执行这个方法得到一个ibinder对象
ibinder binder = (ibinder) method.invoke(null, new object[]{telephony_service});
//转换为具体的服务类(itelephony)接口对象
itelephony telephony = itelephony.stub.asinterface(binder);
//结束通话
telephony.endcall();
//从上是通过 反射来做的, 下面正常的做法>> 按下面来做
// ibinder bindr = servicemanager.getservice(telephony_service);
// itelephony telephony2 = itelephony.stub.asinterface(binder);
// telephony2.endcall();
}catch(exception e){
e.printstacktrace();
}
下面来看看它是怎样的一个机制来操作电话管理功能的.
首先我们要知道所有的服务类都是在与服务管理器交互.服务管理器把所有的服务都纳入到它的管理范畴.而那些系统框架中的各个服务工具类底层都是与这个服务管理器大管家交互.
我是通过 servicemanager这个系统框架提供服务管理器来得到itelephony这个服务的binder对象的
servicemanager è系统框架上的所有服务都在后来运行着.要想得这些服务 就得通过 这个方法getservice来获得
public static ibinder getservice(string name) {//取得服务的ibinder对象
try {
ibinder service = scache.get(name);//先从缓存上找到这个服务的ibinder
if (service != null) {
return service;
} else { //如果为空就通过服务管理器接口实现类的getservice方法来得到你想要的服务
return getiservicemanager().getservice(name);
} catch(remoteexception e) {
log.e(tag, "error in getservice", e);
return null;
public static void addservice(stringname, ibinder service){//往服务管理器中追加一个服务
public static ibinder checkservice(string name)…..//检索已经存在的service
public static string[] listservices() throwsremoteexception//列出当前以有的服务
下面来看看class contextimplextends context类
@override
public object getsystemservice(string name) {
if (window_service.equals(name)) {
returnwindowmanagerimpl.getdefault();
} else if (activity_service.equals(name)) {
returngetactivitymanager();
}else if (telephony_service.equals(name)) {
return gettelephonymanager();
…..以下略
private telephonymanager gettelephonymanager() {
synchronized (msync) {
if (mtelephonymanager == null) {
mtelephonymanager = new telephonymanager(getoutercontext());
return mtelephonymanager;
以下是telephonymanager类的构造方法
/** @hide */
public telephonymanager(context context) {
mcontext = context;
mregistry = itelephonyregistry.stub.asinterface(servicemanager.getservice(
"telephony.registry"));
privateiphonesubinfo getsubscriberinfo() {
// get it each time because that process crashes a lot
returniphonesubinfo.stub.asinterface(servicemanager.getservice("iphonesubinfo"));
private itelephony getitelephony() {
return itelephony.stub.asinterface(servicemanager.getservice(context.telephony_service));
public void listen(phonestatelistener listener, int events) {
string pkgfordebug = mcontext != null ? mcontext.getpackagename() : "<unknown>";
boolean notifynow =(getitelephony() != null);
mregistry.listen(pkgfordebug, listener.callback, events,notifynow);
} catch(remoteexception ex) {
// system process dead
} catch(nullpointerexception ex) {
在telephonymanager类中只是通过服务管理器来得到不同的具体服务者进而提供电话的一些基本信息和注册对电话状态监听的回调, 并没有提供给可以挂断电话的功能
那系统是怎样挂断电话呢…?
最后没有找到有挂断电话的功能方法,但是在itelephony.aidl文件中找到了
/**
* end call or go to the home screen
*
* @return whether it hung up
*/
booleanendcall();
我们知道aidl文件是android接口描述语言。android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信. 为使其他的应用程序也可以访问本应用程序提供的服务,android系统采用了远程过程调用(remote procedure call,rpc)方式来实现.这种以跨进程访问的服务称为aidl(android interface definition language)服务.
这也就是说明我们可以通过此aidl来访问到itelephony这个接口提供的endcall功能.
加载完成后通过itelephony telephony2 = itelephony.stub.asinterface(binder);把binder对象转换成对应的服务端的代理对象,然后就通过这个itelephony服务类在调用端的代理来完成挂断电话的功能.