PhoneStateChanged event handler not executing.

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Can you tell me how to get this PhoneStateChanged handler running?

I call the service from this code:

B4X:
   If FirstTime = True Then
   
      File.WriteString(File.DirDefaultExternal, "MasterControl.txt", "Enabled")

      CallSub2("WidgetService", "ChangeButtonLabel", "Enabled")

      StartServiceAt(ChimeService, DateTime.Now, False)
      StartServiceAt(PhoneState,DateTime.Now, False)
   End If

The service is started but I don't see any Toast messages from the PhoneStateChanged handler when using the phone.

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, tPhoneId As PhoneId
End Sub

Sub Service_Create

    ' Initializes the PhoneStateListener.
    ' All data is also outputted to the filtered log field.
   '------------------------------------------------------
   PE.InitializeWithPhoneState("PE", tPhoneId)
    ToastMessageShow("Starting Phone State Service.",False)
   
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

' Misc Subs.
'-----------
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
   
   Select State
      Case "0" ' Idle state.
          ToastMessageShow("Chimes have now been enabled again.",False)
      
         ChimeService.blnPhoneIsInUse = False
      
      Case "1" ' Ringing state.
          ToastMessageShow("Chimes have now been disabled while the phone is in use.",False)
         
         ChimeService.blnPhoneIsInUse = True

      Case "2" ' Off the hook state.
          ToastMessageShow("Chimes have now been disabled while the phone is in use.",False)
         
         ChimeService.blnPhoneIsInUse = True
         
          ToastMessageShow("blnPhoneIsInUse = " & ChimeService.blnPhoneIsInUse,False)

         ' We don't want the chimes to be interupting the phone when it's being used.
         '---------------------------------------------------------------------------
         If ChimeService.mpMediaPlayer.IsPlaying = True Then
            ChimeService.mpMediaPlayer.Stop            
         End If
         
   End Select
End Sub
 

rleiman

Well-Known Member
Licensed User
Longtime User
Got it working.

Hi,

I got it working.

I found out that I was using the wrong values in the CASE statements. Here is the coding that's working now.

B4X:
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
   
   'ToastMessageShow("The Phone State is: " & State,False)
   
   Select State
      Case "IDLE" ' Idle state.
          ToastMessageShow("Chimes have now been enabled again.",False)
      
         ChimeService.blnPhoneIsInUse = False
      
      Case "RINGING" ' Ringing state.
          ToastMessageShow("Chimes have now been disabled while the phone is in use.",False)
         
         ChimeService.blnPhoneIsInUse = True

      Case "OFFHOOK" ' Off the hook state.
          ToastMessageShow("Chimes have now been disabled while the phone is in use.",False)
         
         ChimeService.blnPhoneIsInUse = True
         
          ToastMessageShow("blnPhoneIsInUse = " & ChimeService.blnPhoneIsInUse,False)

         ' We don't want the chimes to be interupting the phone when it's being used.
         '---------------------------------------------------------------------------
         If ChimeService.mpMediaPlayer.IsPlaying = True Then
            ChimeService.mpMediaPlayer.Stop            
         End If
         
   End Select
End Sub
 
Upvote 0
Top