How can I reject incoming calls in android studio or add them to the blocked contacts list (java)

I am making an Android application that is supposed to block a specific set of numbers that might change over time. I have found a way to detect what number is calling:

    private void blockNumber() {
        String blockNumber = "+359895656436";
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        telephonyManager.listen(new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String phoneNumber) {
                Log.d("phonecalled", state + " " + phoneNumber);
                if(phoneNumber.equals(blockNumber)) {
//                    telephonyService.endCall();
// block number

                }
            }

        }, PhoneStateListener.LISTEN_CALL_STATE);
    }

I have seen solutions such as

https://developer.android.com/reference/android/telecom/Call#reject(boolean,%20java.lang.String).Search Baron

(I have no idea how to obtain the Call object and use it to reject the call)

How to Answer or reject incoming calls programmatically in Android 9+?

(I do not want to make a dealer app)

https://source.android.com/devices/tech/connect/block-numbers#put-blocked-number

which gives me

java.lang.SecurityException: Caller must be system, default dialer or default SMS app

I also tried finding solutions on GitHub but did not find anything.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.