Android Question Services... probably doing something wrong

wimpie3

Well-Known Member
Licensed User
Longtime User
I've created a new app, and added a service module with the following code:
B4X:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

Sub Process_Globals
    Dim Notif As Notification
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
    Notif.Initialize2(Notif.IMPORTANCE_DEFAULT)
    Notif.Icon = "icon"
    Notif.Sound = True
    Notif.Light=True
    Notif.Vibrate = True
    Notif.AutoCancel=True
    Notif.SetInfo2("MyTitle", "Mybody", "MyTag", Main)
    Notif.Notify(9999)
    Service.StartForeground(1,Notif)
    Dim p As Period
    p.Hours = 0
    p.Minutes = 0
    p.Seconds=10
    Dim newDate As Long = DateUtils.AddPeriod(DateTime.Now, p)
    StartServiceAtExact(Me,newDate,True)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub Service_Destroy
End Sub

In my Main I have put:
B4X:
StartService("service")
All I want is showing a message every 10 seconds, even when the app is not running (of course the 10 seconds is only a test).

When I start the app, the service starts and I can clearly see the message appearing every 10 seconds, even when the app itself is not active (when I'm at the homescreen for instance). However, as soon as I kill the app in Android, the service doesn't run anymore. I was expecting the service to continue to run since the service is started again with AtExact... so I'm probably not understanding something here... what am I missing?
 

wimpie3

Well-Known Member
Licensed User
Longtime User
As long as my app is not killed (swiped away), the notification is shown every 10 seconds, so StartServiceAt seems to be working fine with shorter values. Or do you mean that it should be > 15 minutes to keep on working when the app is killed?
 
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
You must have a foreground service if you want your app to be kept alive.
But when I don't want to run a foreground service, and I want to be sure all my notifications ARE shown even when the app is killed... would that mean that the only reliable way to show notifications is to use Firebase Push Notifications?
 
Upvote 0
Top