1

I made an app to block some incoming calls. The app seems running well and done the job. But recently I tried on an android 8.0 phone and crashed.

java.lang.SecurityException: MODIFY_PHONE_STATE permission required.
at android.os.Parcel.readException(Parcel.java:2005)
at android.os.Parcel.readException(Parcel.java:1951)
at com.android.internal.telephony.ITelephony$Stub$Proxy.endCall(ITelephony.java:2027)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:216)
at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod(Reflection.java:802)

So seems the MODIFY_PHONE_STATE permission is required on android 8.0.

I tried to add the permission on the manifest or asking on runtime. No luck.

@Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    numberGlobal = incomingNumber;

                    //Turn ON the mute in case we have to block the call)
                    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                    audioManager.setStreamMute(AudioManager.STREAM_RING, true);

                    if (hasToBeBlocked()) {
                        try {
                            Method method =  telephonyManager.getClass().getDeclaredMethod("getITelephony");
                            method.setAccessible(true);   // true

                            telephonyService = (ITelephony) method.invoke(telephonyManager);
                            telephonyService.silenceRinger();
                            telephonyService.endCall();  // Crashes here only on android 8.0
                            showToast("Ending Call");
                        } catch (Exception e) {
                                     Toast.makeText(context, "AQUI: " + e.toString(), Toast.LENGTH_LONG).show();
                        }
                    }

                    //Turn OFF the mute
                    audioManager.setStreamMute(AudioManager.STREAM_RING, false);

                    break;
                case PhoneStateListener.LISTEN_CALL_STATE:
                    break;
            }
            super.onCallStateChanged(state, incomingNumber);
        }

So what can I do to end a call on android 8.0 (and up)?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Paula Perez
  • 119
  • 2
  • 9

1 Answers1

1

As per the MODIFY_PHONE_STATE documentation:

Not for use by third-party applications.

Therefore your app cannot receive this permission.

As per the endCall documentation:

Companion apps for wearable devices should use the InCallService API instead. Apps performing call screening should use the CallScreeningService API instead.

As per this question, these were both limited to only the default dialer app until Android Q, where now third party apps can request that role.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • You can get it for a personal app if you're willing to root your device and make your app a system app. It just won't work as a play store app – Gabe Sechan May 24 '19 at 22:02