第一步:在程序中新建一个包,包名必须为:com.android.internal.telephony,因为要使用aidl。
第二步:在这个包里面新建一个名为ITelephony.aidl的文件,然后在文件里面写入代码:
<span style="font-size:14px;"> package com.android.internal.telephony;
<span style="white-space:pre"> </span>interface ITelephony{
<span style="white-space:pre"> </span>boolean endCall();
<span style="white-space:pre"> </span>void answerRingingCall();
<span style="white-space:pre"> </span>} </span>
<span style="font-size:14px;">第三步 :通过反射调用endCall方法
* 挂电话 通过反射获得系统挂电话功能
*/
public void endCall() {
//IBinder iBinder = ServiceManager.getService(<strong>TELEPHONY_SERVICE</strong>);
try {
//加载servicemanager的字节码
Class clazz = CallSmsSafeService.class.getClassLoader().loadClass("android.os.ServiceManager");
Method method = clazz.getDeclaredMethod("<strong>getService</strong>", String.class);
IBinder ibinder = (IBinder) method.invoke(null, <strong>TELEPHONY_SERVICE</strong>);
<strong>ITelephony.Stub.asInterface(ibinder).endCall();</strong>
} catch (Exception e) {
e.printStackTrace();
}
}</span>
这里的关键 是通过ServiceManager 的getService()方法拿到TELEPHONY_SERVICE返回的iBinder对象
处于安全考虑 sdk隐藏了ServiceManager 所以只能通过反射来调用方法getService()
最后通过aidl来调用ITelephony的endCall()方法