0

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 ?

Santosh
  • 611
  • 2
  • 6
  • 24

2 Answers2

3

Looks like you forgot adding runtime permission for CALL_PHONE Caused by: java.lang.SecurityException: Neither user 10304 nor current process has android.permission.CALL_PHONE.

user3277530
  • 351
  • 6
  • 14
  • Thank you for your answer. But I added String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.READ_PHONE_STATE}; if (!hasPermissions(this, PERMISSIONS)) { ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL); } – Santosh Dec 15 '17 at 13:54
  • 2
    But... `CALL_PHONE` isn't in your list... – Timmmm Dec 18 '17 at 13:39
  • This was exactly my issue , many thanks – A.Alqadomi Feb 21 '18 at 14:29
0

One possible solution is via Android Accessibility Service. You can find the resource id of Cancel call button and Click that via 11y Service!

Check out this link which uses Android Accessibility Service to press "Force Stop" button just like Greenify App.

How to click button in settings using AccessibilityService?

Hope this helps!

Dhruv Kaushal
  • 632
  • 8
  • 17