【发布时间】:2018-06-08 11:52:41
【问题描述】:
我有一个应用程序使用以下代码来阻止呼叫:
TelephonyManager telephonyManager =
(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
try {
Class<?> telephonyManagerClass =
Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod =
telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
Object iTelephony = getITelephonyMethod.invoke(telephonyManager);
Class<?> iTelephonyClass = Class.forName(iTelephony.getClass().getName());
Method endCallMethod = iTelephonyClass.getDeclaredMethod("endCall");
endCallMethod.setAccessible(true);
endCallMethod.invoke(iTelephony);
} catch (ClassNotFoundException e) {
// ClassNotFoundException
} catch (NoSuchMethodException e) {
// NoSuchMethodException
} catch (IllegalAccessException e) {
// IllegalAccessException
} catch (InvocationTargetException e) {
// InvocationTargetException
} catch (Exception e) {
// Some other exception
}
要工作,这需要 AndroidManifest.xml 中的 android.permission.MODIFY_PHONE_STATE。在 Android M 和 N 中,我还必须向用户请求权限 Manifest.permission.CALL_PHONE。
但现在在 Android 8/Android O 中,上述代码失败并出现 InvocationTargetException: MODIFY_PHONE_STATE permission required
我发现了这个相关的 StackOverflow 帖子:Android - Why does endCall method work but answerRingingCall doesn't work?
这里建议我可以在 PhoneInterfaceManager 上使用反射(而不是 ITelephony,就像我上面所做的那样)并将私有方法 sendRequestAsync(int command) 与结束调用命令一起使用,并通过这样做绕过endCall() 方法中的安全措施。
有没有人尝试过这样的事情?是否可以?我什至如何获得 PhoneInterfaceManager 对象/类而不是 ITelephony?
在结束通话时,我看不出这与 Android N 的代码有什么区别,所以我可能弄错了: https://android.googlesource.com/platform/packages/services/Telephony/+/nougat-release/src/com/android/phone/PhoneInterfaceManager.java
【问题讨论】:
-
请注意,这些东西都不能在 Android P 上运行,如 they are implementing bans on such reflection。
-
它对 Android 8.x 没有帮助,但仅供参考 Android P Beta 2 在 TelecomManager (developer.android.com/reference/android/telecom/…) 中公开了 endCall 方法。我刚刚测试了它,它工作正常。
-
非常感谢您的提醒!不是我的问题的答案,而是让应用程序在未来继续工作的非常有用的信息。
-
你有什么方法可以结束对奥利奥的通话吗? @BjarteAuneOlsen
-
@KishorV:是的,请看下面我的回答。