Android Question PhoneStateChanged event not raised

Nickelgrass

Active Member
Licensed User
Hello,
I have the most simple service with the phone library:
B4X:
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
    Dim pid As PhoneId
End Sub

Sub Service_Create
    pe.InitializeWithPhoneState("PE", pid)
End Sub

Sub Service_Start (StartingIntent As Intent)
    Log("started")
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
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
    pe.StopListening
End Sub

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    Log(State & "  " & IncomingNumber)
End Sub
The "started" appears in the log but when I ring nothing happens. I have added the permission:
AddPermission(android.permission.READ_PHONE_STATE)
and also tried AddPermission("android.permission.READ_PHONE_STATE")
It just simply is not raised althugh the service evidently starts. What do I have to do differently? Does it only work in rooted phones?
Thank you
Best regards
 

HAH

Active Member
Licensed User
I don't see the permission check and request in your code example, How did you added it
 

Nickelgrass

Active Member
Licensed User
Hi,
thanks for the replies.
That link was the example I looked at. I added the permission in the manifest editor.
I would like to use the PhoneStateChange in a service, not an activity. Or is it the runtime permission I need? But how do I add the runtime permission to the service?
Thanks
Best regards
 

DonManfred

Expert
Licensed User
Or is it the runtime permission I need?
yes
But how do I add the runtime permission to the service?
you can define it in starter, BUT you can not request a permission from a service. Services can not show any UI elements.
The request MUST be done from an Activity.
See runtimepermissions tutorial.
 

HAH

Active Member
Licensed User
I would like to use the PhoneStateChange in a service, not an activity. Or is it the runtime permission I need? But how do I add the runtime permission to the service?
Runtime permission means real user consent acceptance, it can't be invisible.
 

Nickelgrass

Active Member
Licensed User
Thanks it worked with the runtime permission. I guess I am not quite used to those.
Two other short questions.
If the Service is not a foreground service and it gets killed by the system will it still be woken up when the event is risen?
Also if I define a list in the ProcessGlobals that is loaded from a file in the ServiceCreate sub will this list be kept even if the service is not a foreground service or should I reload it in the ServiceStart sub?
 

Erel

Administrator
Staff member
Licensed User
If the Service is not a foreground service and it gets killed by the system will it still be woken up when the event is risen?
No. See this tutorial: https://www.b4x.com/android/forum/threads/20103/#content

Also if I define a list in the ProcessGlobals that is loaded from a file in the ServiceCreate sub will this list be kept even if the service is not a foreground service or should I reload it in the ServiceStart sub?
Load it in Service_Create and it will be kept alive until the process is killed.
 

Nickelgrass

Active Member
Licensed User
Thanks for the reply.

I tried to understand the Service killing. How I understand it if I want to reliably and over a long period want to monitor the incoming calls I need a foreground service. Otherwise the service may be killed. But the downside is, that I need to display a notification for that.
Is there no way to make a static broadcast receiver for an incoming call that would start the service if needed? Or is there any other way to reliably monitor the phonecalls without the need for a notification?
 

Nickelgrass

Active Member
Licensed User
Ok, thanks, I will try it.

But I thought the "dangerous" permissions could only be set as dynamic filter. Did I missunderstand something?

Update: works very nicly with SMS and phonecalls in one service with static filter.
 
Last edited:
Top