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)?