简体   繁体   English

如何以编程方式在Android Nougat,Oreo及更高版本中阻止通话?

[英]How to Block Call in Android Nougat, Oreo and above programatically?

This code works fine till Android 6 ( Marshmallow ): 这段代码在Android 6Marshmallow )之前可以正常工作:

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 阅读文档后

I called endCall(context, number) still, Unable to block call. 我仍然打电话给endCall(context, number) ,无法阻止通话。 Need working solution in Android Nougat, Oreo, Pie and above. 需要Android Nougat,Oreo,Pie及更高版本的工作解决方案。

Update 更新

I included following code from official Nougat documentation but still, the number was not added into blocklist. 我包括了来自牛轧糖官方文档的以下代码,但仍然没有将该数字添加到阻止列表中。

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 can not use non-SDK interfaces Android 9不能使用非SDK界面

Android 9 introduces new restrictions on the use of non-SDK interfaces, whether directly, via reflection, or via JNI. Android 9引入了对非SDK接口使用的新限制,无论是直接,通过反射还是通过JNI。 These restrictions are applied whenever an app references a non-SDK interface or attempts to obtain its handle using reflection or JNI. 每当应用程序引用非SDK接口或尝试使用反射或JNI获取其句柄时,都会应用这些限制。

And it looks like you can not read or write to BlockedNumberContract unless it's a system app or default dialer or default sms app 除非您是系统应用程序或默认拨号程序或默认短信应用程序,否则您似乎无法读写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. 只有系统,默认的SMS应用程序和默认的电话应用程序(请参阅TelecomManager.getDefaultDialerPackage())和运营商应用程序(请参阅CarrierService)可以读取和写入阻止号码提供程序。 However, canCurrentUserBlockNumbers(Context) can be accessed by any application. 但是,canCurrentUserBlockNumbers(Context)可以由任何应用程序访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM