Up to Android 7.1 it was possible to end an incoming call by using the ITelephony.endCall() method and giving your app the permissions android.permission.CALL_PHONE and android.permission.READ_PHONE_STATE.
When doing the same on Android 8.0 Oreo (API 26), i get this error
12-14 17:37:26.160 20962-20962/com.xinthe.carmode W/System.err: java.lang.reflect.InvocationTargetException 12-14 17:37:26.161 20962-20962/com.xinthe.carmode W/System.err: at java.lang.reflect.Method.invoke(Native Method) 12-14 17:37:26.161 20962-20962/com.xinthe.carmode W/System.err: at com.xinthe.carmode.listeners.MyPhoneStateListener.disconnectCall(MyPhoneStateListener.java:108) 12-14 17:37:26.161 20962-20962/com.xinthe.carmode W/System.err: at com.xinthe.carmode.listeners.MyPhoneStateListener.onCallStateChanged(MyPhoneStateListener.java:51) 12-14 17:37:26.161 20962-20962/com.xinthe.carmode W/System.err: at android.telephony.PhoneStateListener$1.handleMessage(PhoneStateListener.java:338) 12-14 17:37:26.161 20962-20962/com.xinthe.carmode W/System.err: at android.os.Handler.dispatchMessage(Handler.java:105) 12-14 17:37:26.161 20962-20962/com.xinthe.carmode W/System.err: at android.os.Looper.loop(Looper.java:164) 12-14 17:37:26.161 20962-20962/com.xinthe.carmode W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6809) 12-14 17:37:26.161 20962-20962/com.xinthe.carmode W/System.err: at java.lang.reflect.Method.invoke(Native Method) 12-14 17:37:26.161 20962-20962/com.xinthe.carmode W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 12-14 17:37:26.161 20962-20962/com.xinthe.carmode W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 12-14 17:37:26.162 20962-20962/com.xinthe.carmode W/System.err: Caused by: java.lang.SecurityException: Neither user 10304 nor current process has android.permission.CALL_PHONE. 12-14 17:37:26.162 20962-20962/com.xinthe.carmode W/System.err: at android.os.Parcel.readException(Parcel.java:1942) 12-14 17:37:26.162 20962-20962/com.xinthe.carmode W/System.err: at android.os.Parcel.readException(Parcel.java:1888) 12-14 17:37:26.162 20962-20962/com.xinthe.carmode W/System.err: at com.android.internal.telephony.ITelephony$Stub$Proxy.endCall(ITelephony.java:1955) 12-14 17:37:26.162 20962-20962/com.xinthe.carmode W/System.err: ... 10 more 12-14 17:37:26.162 20962-20962/com.xinthe.carmode E/End call error: FATAL ERROR: could not connect to telephony subsystem 12-14 17:37:26.162 20962-20962/com.xinthe.carmode E/End call error: Exception object: java.lang.reflect.InvocationTargetException
Here is the code for READ_PHONE_STATE run time permission.
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ANSWER_PHONE_CALLS};
void permissionCheck() {
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
}
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
Can anyone please help me ?