Android Question [SOLVED] PhoneStateChanged of PhoneEvents in Phone library - Can it be used in the Starter service?

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I using PhoneStateChanged of PhoneEvents from the Phone library for the first time for a new user requirement. To better understand how it works I found a few references to code samples in the forum. Based on those samples, I made this simple app. I added the coding to the Starter service. Maybe that's where I went wrong. I rang my mobile to see if it work but the logs don't show that the phone state sub is responding. There are no error messages in the Logs and they show the Starter service is running. Can you let me know if I can do this in the Starter service and what coding changes I need to make? It's a super simple app but I know I'm missing something. Maybe in the manifest editor?

Main Module:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Starter Service Module:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals

    Dim PhoneId As PhoneId
    Dim PE As PhoneEvents
End Sub

Sub Service_Create

    PE.InitializeWithPhoneState("PE", PhoneId)
    Log("Starter service created.")
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub

Sub Service_TaskRemoved

End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    Log("This phone is " & State & ".")
End Sub
 

rleiman

Well-Known Member
Licensed User
Longtime User
I think this might be of interest ... Phone State Changed Event Not Raised ...
You hit the nail on the head!

Thanks for the link!

I never thought of asking the user for permissions because the logs didn't show any errors. Just like in the post link you gave me, they had the same issue. Here's the coding now which works.

Now I can add the coding to all of my apps that use sound that was supposed to be silent when the user is on a call. My customers complained that the app started to make sounds when they were on phone calls after they upgraded to the latest versions of Android. ?

Anyway, thanks so much. :)

Main Module:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume

    If Starter.rp.Check(Starter.rp.PERMISSION_READ_PHONE_STATE) = False Then
        Sleep(100)
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_PHONE_STATE)
        Wait For Activity_PermissionResult (permission As String, Result As Boolean)
        Log(permission)
        If Result = False Then
            Msgbox("The app asked for permission to read the phone state. This permission is " & _
                "needed to allow the app to mute app sounds when you are on a call. " & _
                "If you previously selected 'Don't ask again' then go to screen that has all " & _
                "of your apps. Locate this app and long press on the app icon. A screen  " & _
                "will pop up. Tap App info and scroll until you find Permissions and tap it." & _
                "You will find the Phone permission toggle. Tap it so it's 'On'", _
                "Please allow this permission")
            
            Activity.Finish

        End If
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Starter Service Module:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals

    Dim PhoneId As PhoneId
    Dim PE As PhoneEvents
    Public rp As RuntimePermissions
End Sub

Sub Service_Create

    PE.InitializeWithPhoneState("PE", PhoneId)
    Log("Starter service created.")
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub

Sub Service_TaskRemoved

End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    Log("This phone is " & State & ".")
End Sub
 
Upvote 0
Top