简体   繁体   中英

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

This code works fine till 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

I called endCall(context, number) still, Unable to block call. Need working solution in Android Nougat, Oreo, Pie and above.

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 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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