PhoneStateListener not working as expected

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I'm just starting to learn how to use the PhoneStateListener but can not figure out how to get it to recognize the event that takes place when the phone rings and when it's in use like when the user is on a phone call.

I place this code in a service and it works only when initially starting the app. The code that executes is the one that states the phone is in Idle state (0) but can't trap the events for Phone Ringing (1) and Off the Hook (2).

Can you tell me what I did wrong?

Perhaps someone has a working simple project that they can upload so I can learn the correct way to use the PhoneStateListener.

Thanks.

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   ' Declares the phone state listener.
   '-----------------------------------
    Dim PSL As PhoneStateListener
End Sub

Sub Service_Create

    ' Initializes the PhoneStateListener.
    ' All data is also outputted to the filtered log field.
   '------------------------------------------------------
   PSL.Initialize("PSL", False)
    
    ' Starts listening.
   '------------------
    PSL.startListening()
   ' ToastMessageShow(PSL.CellLocation,False)
    
    'Remember: Find all information about the returns here:
    ' http://developer.android.com/reference/android/telephony/PhoneStateListener.html
End Sub

Sub PSL_onCallStateChanged (State As Int, incomingNumber As String)
   
   Select State
      Case "0"
                ToastMessageShow("The phone state is idle.",False)
      
      Case "1"
          ToastMessageShow("The phone state is ringing.",False)

      Case "2"
          ToastMessageShow("The phone state is off the hook.",False)
   End Select
End Sub
 
Last edited:

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Thanks for the tutorial links. I will go through them to see how to do it.

All the best to you. :)
 
Upvote 0
Top