2

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);
cooldev
  • 419
  • 1
  • 4
  • 13
  • 1
    @jackjay The link you provided is broken, I believe you meant [this](https://source.android.com/devices/tech/connect/block-numbers). – Filnor Aug 29 '18 at 07:14
  • This is corrected link: https://source.android.com/devices/tech/connect/block-numbers I'm implementing it now. – cooldev Aug 29 '18 at 07:15
  • yes you should follow this link. – jack jay Aug 29 '18 at 08:06
  • I only wanted to put numbers in blacklist hence, I wrote this: ` ContentValues values = new ContentValues(); values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "1234567890"); Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values); ` But that's not putting the numbers into blocklist – cooldev Aug 29 '18 at 09:58

1 Answers1

0

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.

Aeiman Bakeer
  • 490
  • 5
  • 8