2

I am building an application that provides a way for users to block all incoming calls once they activate service.

There is a clear lack of solutions to the problem when it comes to targeting newer SDK versions, in my case Oreo and above. The closest I have been to a solution is this thread but there is no accepted answer to the question, nor do any of the provided answers work in my case.

Here are just some of the links that I have also visited:

My approach is to use the foreground service and listen to phone call state changes.

@Override
public void onCreate() {
    super.onCreate();
    TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(new CallStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
    Log.d(TAG, "Called onCreate() method");
}

where this is the CallStateListener

private class CallStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String phoneNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            Log.d(TAG, "Phone is ringing");
            TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
            try {
                //Gain access to ITelephony getter
                Class c = Class.forName(telephonyManager.getClass().getName());
                Method method = c.getDeclaredMethod("getITelephony");
                method.setAccessible(true);
                com.android.internal.telephony.ITelephony telephony = (ITelephony) method.invoke(telephonyManager);

                //Hung up the call
                telephony.endCall();
            } catch (Exception e) {
                Log.d(TAG, "Exception occurred: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
}

I can confirm that the log message Phone is ringing is received

The exception I obtain is Exception occurred: MODIFY_PHONE_STATE permission required.

Permission MODIFY_PHONE_STATE is not available to third-party application developers. I have seen a suggestion to modify the linter, which I did, but I still cannot request this permission and so the exception persists.

Is there an alternative way to hang up calls in these SDK versions? If so, I would love to be appointed in the right direction. Thank you.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

0 Answers0