Hello All
I am struggling to keep background services running correctly on Android 8+, I have already use this https://www.b4x.com/android/forum/threads/background-location-tracking.99873/#content and in the settings on the device enabled the app to run in the background.
Has anyone successfully got BG tasks running on android 8+, I would like to hear from you.
Some strange things:
1. I have a timer in the service will gets ticked every 2 seconds.
2. In the timer I update the notification every 10 seconds, it also tracks the time when it should occur, and if it is out by more then 30 seconds a notification is triggered, and it does happen. Its like the service is being paused.
Here is the service code:
Regards
John.
I am struggling to keep background services running correctly on Android 8+, I have already use this https://www.b4x.com/android/forum/threads/background-location-tracking.99873/#content and in the settings on the device enabled the app to run in the background.
Has anyone successfully got BG tasks running on android 8+, I would like to hear from you.
Some strange things:
1. I have a timer in the service will gets ticked every 2 seconds.
2. In the timer I update the notification every 10 seconds, it also tracks the time when it should occur, and if it is out by more then 30 seconds a notification is triggered, and it does happen. Its like the service is being paused.
B4X:
Sub tmrServiceHook_tick
' updateNotification is set to now time when the service start
' // update the notification to keep everything alive
' // https://www.b4x.com/android/forum/threads/background-services-in-android-8-observations.103533/
If updateNotification < DateTime.Now Then
Dim secondsDiff As Period = DateUtils.PeriodBetween(updateNotification, DateTime.now)
' // log by exception
If secondsDiff.Seconds > 30 Then
mod_functions.writelog("svc_service(), tmrServiceHook_tick, Still Alive, running, abnormally, Seconds " & secondsDiff.Seconds)
Dim n5 As Notification
n5.Initialize
n5.Icon = "app_logo_4_notification_bar_issue"
n5.Number = 999
n5.Insistent = True
' // always last prop to be set
n5.SetInfo("ATLAS HELLO WARNING", "DEVICE IS PREVENTING NORMAL OPERATION", Main)
n5.Notify(999)
End If
updateNotification = DateTime.Now + (DateTime.TicksPerSecond * 10)
mod_functions.writelog("svc_service(), tmrServiceHook_tick, Still Alive, Next Marker @ " & DateTime.Time(updateNotification))
settNotification = 1 ' this is checked further on in this function for a non zero value and does the notification.
End If
Here is the service code:
B4X:
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
nNewMessage.Initialize
PE.InitializeWithPhoneState("PE", PhoneId)
' // set the service hook to be called every 2 seconds
tmrServiceHook.Initialize("tmrServiceHook",2000)
End Sub
Sub Service_Start (StartingIntent As Intent)
' // setup the notification
Service.StartForeground(51042,CreateNotification("Atlas Hello","Offline","app_logo_4_notification_bar_offline",Main,False,False))
pw.PartialLock
mlWifi.holdWifiOn
' // main data is not available
If Not(Main.APPSET.IsInitialized) Then
mod_functions.writelog("svc_service(), Service_Start, Death is so painful, we have been Destroyed ! , restarting app")
StartServiceAt(svc_bootup,DateTime.Now + (3 * DateTime.TicksPerSecond),True)
kill_me
Else
mod_functions.writelog("svc_service(), Service_Start")
tmrServiceHook.Enabled = True
End If
updateNotification = DateTime.now
End Sub
Regards
John.