Android Question Detect incoming phone call?

U

unba1300

Guest
Is there a way to detect an incoming phone call? I need to stop text-to-speech within my app if the phone rings. Thanks.
 

mangojack

Well-Known Member
Licensed User
Longtime User
You could use PhoneEvents to listen to phone state changes.

B4X:
Dim PE As PhoneEvents

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)

  Select State
     'Case "IDLE" ' Idle state.
  
      Case "RINGING" ' Ringing state.
  
       'your code ...
 
Upvote 0
U

unba1300

Guest
I put this in Main Activity:
B4X:
If FirstTime Then
    StartService(sPhone)
and then created a Service Module called sPhone with:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim PE As PhoneEvents
End Sub
Sub Service_Create
    PE.Initialize("PE")
End Sub
Sub Service_Start (StartingIntent As Intent)

End Sub
Sub Service_Destroy

End Sub
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    Log("PhoneStateChanged, State = " & State & ", IncomingNumber = " & IncomingNumber)
    Msgbox("PhoneStateChanged, State = " & State & ", IncomingNumber = " & IncomingNumber, "")
'    Log(Intent.ExtrasToString)
End Sub
but I don't get any message when calling the phone while my app is active.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Try ..
B4X:
Sub Process_Globals

   Dim PE As PhoneEvents
   Dim PhoneId As PhoneId
 
End Sub

Sub Service_Create
 
   PE.InitializeWithPhoneState("PE",PhoneId)

to be honest .. don't understand requirement of PhoneID ?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
to be honest .. don't understand requirement of PhoneID ?
This is a small trick that was used before the manifest editor was available. By creating a PhoneId object you are forcing the compiler to add the android.permission.READ_PHONE_STATE permission. This permission is required in order to intercept incoming connections. However I didn't want to add this permission to PhoneEvents as the other events do not require it. So only if you need the phone state events then the permission is added.
 
Upvote 0
U

unba1300

Guest
Thank you for your help. I am now able to stop TTS, and set some other flags, when the phone rings. I'm just wondering, is there anything negative about having this service always running, like increased memory usage? It's the first time I'm using a service. And will the OS stop the service after the app is exited for a while?

Next challenge is to do the same (mainly stop TTS) when the user presses the Home key. I thought it would be easy with KeyCodes, but apparently not possible. I know I could do this in Activity_Pause, but then it would also stop TTS during an orientation change, which I don't want to do.
 
Last edited by a moderator:
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Unun , to stop the OS killing the service ... in Service_Create
B4X:
Service.StartForeground(1,sNotif)
I've uploaded small service example which might be useful as reference , (see Service_Create regarding StartForeground parameters)
requires Answer Call Library ...

From what I have read, It is not possible to trap the Home key.

I do not believe this service always running is a major issue , power/memory wise ... maybe the experts can comment.
 

Attachments

  • AutoAnswerExample.zip
    51.5 KB · Views: 779
Last edited:
Upvote 0
Top