Android开发的时候遇到一个拦截来电的需求,但是挂断电话的 endCall() 方法被Google给隐藏了,需要先导入aidl文件并使用反射来获取对应的对象。
首先下载两个aidl文件:ITelephony.aidl NeighboringCellInfo.aidl
放入aidl目录并创建相应的包。
查看 TelephonyManager 的源码,搜索 endCall()
1 2 3 4 5 6 7 8 9 10 11 12 |
/** @hide */ @SystemApi public boolean endCall() { try { ITelephony telephony = getITelephony(); if (telephony != null) return telephony.endCall(); } catch (RemoteException e) { Log.e(TAG, "Error calling ITelephony#endCall", e); } return false; } |
可以看到调用了getITelephony() 方法
1 2 3 4 5 6 |
/** * @hide */ private ITelephony getITelephony() { return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE)); } |
遗憾的是该方法被私有化了,但是我们有反射这一大杀器。
现在有两种方案去获取 ITelephony 对象,一种是直接反射 TelephonyManager 的 getITelephony() 方法,另一种是 反射 ServiceManager 的 getService 方法,可以选一种你喜欢的。
方案一:
1 2 3 4 5 6 |
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); Class<?> clazz = Class.forName(telephonyManager.getClass().getName()); Method method = clazz.getDeclaredMethod("getITelephony"); method.setAccessible(true); ITelephony iTelephony = (ITelephony) method.invoke(telephonyManager); iTelephony.endCall(); |
方案二:
1 2 3 4 5 |
Class<?> clazz = Class.forName("android.os.ServiceManager"); Method method = clazz.getMethod("getService", String.class); IBinder iBinder = (IBinder) method.invoke(null, Context.TELEPHONY_SERVICE); ITelephony iTelephony = ITelephony.Stub.asInterface(iBinder); iTelephony.endCall(); |
- 本文固定链接: https://blog.kuoruan.com/100.html
- 转载请注明: Index 于 扩软博客 发表
捐 赠如果您觉得这篇文章有用处,请支持作者!鼓励作者写出更好更多的文章!