此代码在 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);
我仍然调用了 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/