【问题标题】:ITelephony.endCall() doesn't work anymore for blocking calls in Android 8/Android O/API 26ITelephony.endCall() 不再适用于阻止 Android 8/Android O/API 26 中的呼叫
【发布时间】: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?

我认为这是有问题的源代码:https://android.googlesource.com/platform/packages/services/Telephony/+/oreo-release/src/com/android/phone/PhoneInterfaceManager.java

在结束通话时,我看不出这与 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:是的,请看下面我的回答。

标签: android android-8.0-oreo


【解决方案1】:

感谢 Nikola Brežnjak 的博客,我找到了答案:https://dev.to/hitman666/how-to-make-a-native-android-app-that-can-block-phone-calls--4e15

基本上,您需要在项目中包含 ITelephony 接口。使用以下代码创建文件 ITelephony.class(保留命名空间):

package com.android.internal.telephony;

public interface ITelephony {
    boolean endCall();
    void answerRingingCall();
    void silenceRinger();
}

然后从您的代码中,使用以下代码结束通话:

// This is only useful on Android O and older
// Needs permission CALL_PHONE
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O
    || checkSelfPermission(Manifest.permission.CALL_PHONE)
        == PackageManager.PERMISSION_DENIED) {
    return;
}

try {
    ITelephony telephonyService;

    TelephonyManager telephonyManager = (TelephonyManager)
            getSystemService(Context.TELEPHONY_SERVICE);

    Method getITelephony = telephonyManager
            .getClass()
            .getDeclaredMethod("getITelephony");

    getITelephony.setAccessible(true);

    telephonyService = (ITelephony) getITelephony.invoke(telephonyManager);

    telephonyService.endCall();

} catch (Exception ignored) {
}

【讨论】:

  • Android Pie 内置了结束通话的解决方案,因此您不必使用反射: TelecomManager teleManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);电信管理器.endCall();
  • 但是 8.0.1 呢,你不能在 O 上使用反射,新的解决方案可以在 P 上使用?
  • 我在 Lollipop 上执行此操作,该方法存在于此处。我拥有所谓的正确权限,并且我将应用程序作为系统应用程序运行并具有 root 权限。但这会冻结整个应用程序。我可以接听或挂断电话,但随后应用程序冻结。你知道原因吗?
  • @Pankaj:上述方法(带有反射)应该适用于 Android 8.1 (Oreo) 及以下版本。如果我没记错的话,我想我已经尝试过到 Android 6.0 (Marshmallow)。
  • @DADi590:我不确定它为什么会冻结,但正如您在我最初的问题中的所有尝试捕获所看到的那样,使用反射时有很多事情会失败:-)
猜你喜欢
  • 1970-01-01
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
相关资源
最近更新 更多