Android Question killcall no longer works. can anybody help?

mhk1368

Member
hi
I tried to reject the call with the following program in Android 11, but it does not work and gives the following error.( r.RunMethod("endCall"))
java.lang.NoSuchMethodException: java.lang.Object.endCall []
I just need a piece of code that can reject the call.

example:
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent1 As Intent)
        
    If State="RINGING" Then
        'Squillo
        if IncomingNumber ="0039333xxxx" then KillCall   
    Else If State="OFFHOOK" Then
        ' OFF Hook
    else If State="IDLE" Then
        'IDEL
        
    End If
End Sub

Sub KillCall
   Dim r As Reflector
   r.Target = r.GetContext
   Dim TelephonyManager, TelephonyInterface As Object
   TelephonyManager = r.RunMethod2("getSystemService", "phone", "java.lang.String")
   r.Target = TelephonyManager
   TelephonyInterface = r.RunMethod("getITelephony")
   r.Target = TelephonyInterface
   r.RunMethod("endCall")
End Sub
 

agraham

Expert
Licensed User
Longtime User
Has this worked before on earlier versions of Android? If so then I think these are non documented API calls that Google are now blocking the use of. Google wants apps to use only documented public SDK APIs. I don't think apps that are not the default telephony app on later versions of Android can handle calls.

 
Upvote 0

mhk1368

Member
Has this worked before on earlier versions of Android? If so then I think these are non documented API calls that Google are now blocking the use of. Google wants apps to use only documented public SDK APIs. I don't think apps that are not the default telephony app on later versions of Android can handle calls.

Hi and Thanks bro
can you post a sample code for B4a so I can do this with my phone's default app?(samsung)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Magma

Expert
Licensed User
Longtime User
As I remember the only way to use it right... was a simple trick to take incoming number.. right... at second ring...and then..

At first ring the default phone app taking control and leaving incoming as unknown... so at second and after the number is readable..
 
Upvote 0

mhk1368

Member
As I remember the only way to use it right... was a simple trick to take incoming number.. right... at second ring...and then..

At first ring the default phone app taking control and leaving incoming as unknown... so at second and after the number is readable..
hi bro
I can get the phone number, but my problem is that after that I want to hang up and none of the commands do that. I just need a b4a example that does this.
b4a:
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)

    Log("Phone state: " & State)
      
    If State = "RINGING" Then
        Log("The incomming number is: " & IncomingNumber)
        If Not(IncomingNumber="") Then
'        hangup command

        End If
    Else
    End If
End Sub
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Hi there...
Ok found my code that works... in Android 9,10,11,12... for sure..

at my manifest have this: android:targetSdkVersion="30"

and ofcourse asking permissions and in my app and from manifest for these:
AddPermission("android.permission.CALL_PHONE")
AddPermission("android.permission.READ_PHONE_STATE")
AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.ANSWER_PHONE_CALLS")
AddPermission("android.permission.READ_CALL_LOG")
AddPermission("android.permission.READ_PHONE_NUMBERS")

B4X:
Sub ENDCALL
  If howend="EndCall" Then 'AVER>27
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim tm As JavaObject = ctxt.RunMethod("getSystemService", Array("telecom"))
    Dim success As Boolean = tm.RunMethod("endCall", Null)
    If success=False Then
            Dim success As Boolean = tm.RunMethod("endCall", Null)
    End If
    Else
        KillCall 'my old sub !
  End If
End Sub


but I will suggest you again (because your app will not be default phone app) to take after the 2nd ring the incoming number... i achieved it like this:
B4X:
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)

    ring=ring+1
    Log(ring)
    If ring<2 Then
        Return
        Else
        ring=0
    End If
    Log("passing to check...")

    If State = "RINGING" Then
    ph=IncomingNumber.trim
...
 
Upvote 0

mhk1368

Member
Hi there...
Ok found my code that works... in Android 9,10,11,12... for sure..

at my manifest have this: android:targetSdkVersion="30"

and ofcourse asking permissions and in my app and from manifest for these:
AddPermission("android.permission.CALL_PHONE")
AddPermission("android.permission.READ_PHONE_STATE")
AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.ANSWER_PHONE_CALLS")
AddPermission("android.permission.READ_CALL_LOG")
AddPermission("android.permission.READ_PHONE_NUMBERS")

B4X:
Sub ENDCALL
  If howend="EndCall" Then 'AVER>27
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim tm As JavaObject = ctxt.RunMethod("getSystemService", Array("telecom"))
    Dim success As Boolean = tm.RunMethod("endCall", Null)
    If success=False Then
            Dim success As Boolean = tm.RunMethod("endCall", Null)
    End If
    Else
        KillCall 'my old sub !
  End If
End Sub


but I will suggest you again (because your app will not be default phone app) to take after the 2nd ring the incoming number... i achieved it like this:
B4X:
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)

    ring=ring+1
    Log(ring)
    If ring<2 Then
        Return
        Else
        ring=0
    End If
    Log("passing to check...")

    If State = "RINGING" Then
    ph=IncomingNumber.trim
...
hi and thanks
But after testing it gives the following errors(Dim success As Boolean = tmm.RunMethod("endCall", Null))
error:
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:479)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:293)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.phone.PhoneEvents$ActionHandler$1.run(PhoneEvents.java:338)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8293)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1045)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
hi and thanks
But after testing it gives the following errors(Dim success As Boolean = tmm.RunMethod("endCall", Null))
error:
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:479)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:293)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.phone.PhoneEvents$ActionHandler$1.run(PhoneEvents.java:338)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8293)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1045)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
...hmm strange... at me not having errors like this...

can you upload a small project (to reproduce error) to check it ?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I'm puzzled how @Magma code can work. Maybe it's his phone has an earlier Android version. I've looked inside android.jar for SDK 30 and there is no public endCall method that I can see in TelephonyManager. It would have to be a public method for javaObject call it if it existed. As @DonManfred says above it was deprecated in API 29 and it looks like it is removed in 30.

 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Ah! I see it now. The OP was using, and I was looking, at TelephonyManager. Magma is using TelecomManager - note the difference. EndCall does exist in Android 12 TelecomManager. I can call it using Magma's code but I get a security exception as I haven't set up the permissions. Try it with Magma's code to get TelecomManager.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
thanks a lot bro
can u explain what is my mistake?
You had the ENDCALL inside the if for rings>2... before the "if incoming.number" ---> you must had inside this if.... because sometimes even 2 rings not enough to get incoming number because of the default phone app !

But if you have the counter and the if... for me is better to have "if" with state (RINGING etc..) and then the endcall.. it is safer and you will not have errors !

I ve changed the permissions because you had a lot and I was boring to read them, also I don;t know arabic :) ... also change the manifest too...
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
ps: Also have in mind that the endCALL (telecom) works for SDK >27... before that need KillCall you had :) (but also count the rings... because in those versions - you get faster the number... but if not count them you will loose the number of call logs - that is very bad)
 
Upvote 0
Top