java - 如何以编程方式阻止 Android Nougat、Oreo 及更高版本中的调用?

标签 java android android-studio android-phone-call callblocking

此代码在 Android 6 (Marshmallow) 之前工作正常:

TelephonyManager tm = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
Object telephonyService = m.invoke(tm); 
c = Class.forName(telephonyService.getClass().getName()); 
m = c.getDeclaredMethod("endCall"); 
m.setAccessible(true); 
m.invoke(telephonyService);

After reading documentation

我仍然调用了 endCall(context, number),无法阻止调用。需要适用于 Android Nougat、Oreo、Pie 及更高版本的解决方案。

更新

我包含了官方 Nougat 文档中的以下代码,但该号码仍未添加到黑名单中。

Cursor c = mContext.getContentResolver().query(BlockedNumberContract.BlockedNumbers.CONTENT_URI,
                    new String[]{BlockedNumberContract.BlockedNumbers.COLUMN_ID,
                            BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER,
                            BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER}, null, null, null);

最佳答案

Android 9不能使用非SDK接口(interface)

Android 9 introduces new restrictions on the use of non-SDK interfaces, whether directly, via reflection, or via JNI. These restrictions are applied whenever an app references a non-SDK interface or attempts to obtain its handle using reflection or JNI.

看起来你无法读取或写入 BlockedNumberContract 除非它是系统应用程序或默认拨号器或默认短信应用程序

Permissions

Only the system, the default SMS application, and the default phone app (See TelecomManager.getDefaultDialerPackage()), and carrier apps (See CarrierService) can read, and write to the blockednumber provider. However, canCurrentUserBlockNumbers(Context) can be accessed by any application.

关于java - 如何以编程方式阻止 Android Nougat、Oreo 及更高版本中的调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070776/

相关文章:

java - 按照与目录中相同的顺序获取输入文件

android - Qt/QML Android发送自定义Intent(分享URL)的最佳实践

安卓倒车动画

java - Axon 夹具注入(inject)在 @CommandHandler 注解的方法中失败(构造函数除外)

java - 如何区分复选框字段和一组单选按钮

java - 不使用 Intent 将事件插入日历错误。安卓工作室

javascript - 请求许可/Android Studio 时应用程序崩溃

android - com.android.build.api.transform.TransformException : java. util.zip.ZipException

java - BLE设备与同一设备上不同Android应用程序之间的通信

android - Android 版 Cordova 构建失败