Android Question Check if a phone call is in progress?

Inman

Well-Known Member
Licensed User
Longtime User
I am working on an app that is required to vibrate when an incoming phone call comes. I detected incoming call using PhoneEvents_PhoneStateChanged function, checking it's STATE=RINGING. I can vibrate the device with PhoneVibrate as well. But I want to make sure I don't vibrate on an incoming call if the user is already on a phone call.

So my question is how to detect if a phone call is already in progress. I see a java code in the stackoverflow post below but it is from 2011. I hope it still works. Could you please translate it to B4A?

https://stackoverflow.com/questions/7766434/android-api-to-check-if-call-is-active-or-on-hold
 

Inman

Well-Known Member
Licensed User
Longtime User
Just looking at this updated thread ... https://www.b4x.com/android/forum/t...ed-sample-all-possible-calling-states.104204/

If you are already detecting Phone State changed events , can you not detect if the phone is "OFFHOOK" ?

... and if this is true , do not Vibrate.

Problem is when the user is already on call and then another call comes (i.e. call waiting), State=RINGING even though a call is in progress. Which why I need a way to doublecheck to make sure a call is not in progress.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
With the help of Erel's code given here, I managed to do it myself
B4X:
Sub CallInProgress As Boolean
    Dim r As Reflector, flag As Boolean
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
    Dim mode As Int
    mode=r.RunMethod("getMode")
    If mode=2 Then flag=True Else flag=False
    Return flag
End Sub

In fact with different values of mode, you can get various telephony data

Mode = 0, Normal audio mode: not ringing and no call established.
Mode = 1, Ringing audio mode. An incoming is being signaled.
Mode = 2, In call audio mode. A telephony call is established.
Mode = 3, In communication audio mode. An audio/video chat or VoIP call is established.
 
Upvote 0
Top