Android Question [COULD BE SOLVED]Huawei Y6 Android 8 - Background Services not operating correctly

Jmu5667

Well-Known Member
Licensed User
Longtime User
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.

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.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
A timer will have zero effect on this.

The correct way to keep an app running in the background is with a foreground service as demonstrated in the background location tracking.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Well,
if you want to see the delay issue with Firebase you should setup a small project following the tutorial.

About the other method, following all what I'm reading everywhere and here in this thread, I have setup a small project that every 5 seconds make a RDC connection with my server. It's just an idea where to start. If it works regular and forever (= when the device sleeps) then I can generate local notifications according to what my server say and abandon Firebase.
As I told it seems to work, but here peolple says that sooner or later I will get into trouble (app killed by OS, etc)
 

Attachments

  • test.zip
    19.2 KB · Views: 228
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
So you did like this, more or less as my sample and you worry about nothing ? Regardless what experts say here ?
Would be very frustrating spend time and then fall in trouble again ....

@Erel: the websocket sample looks a bit old. I see for example a "android.app.Service.START_STICKY" that make me worry ...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is indeed old. You should remove the sticky service and make sure that there is a foreground service (I don't remember).

Everything was already said several times. Some devices will let an app with foreground service run forever (you also need to prevent the device from going to sleep with PhoneWakeState) and some devices won't. It will not be more reliable than Firebase push notifications.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Anyway: did you use a timer as in my sample to make periodic http request to your server ? Which interval did you choose ?
Or is your solution similar to the websocket sample that linked Erel ?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Thank you very much Jmu5667...

I've completed the code for managing notifications with a WebSocket as Erel suggested to try.
The device ping the server every minute, so the server always knows who is online or not.
The background service is running since several days. The OS says nothing and doesn't kill nothing. Also I don't appreciate any increase in power consumption. There must be for sure if the connection is kept alive 24h24, but I don't note it.
Hope I can consider this a reliable solution. Anyway I put the user in the condition to switch back to FireBase notifications if he experience troubles.
Thank you
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Thank you very much Jmu5667...

I've completed the code for managing notifications with a WebSocket as Erel suggested to try.
The device ping the server every minute, so the server always knows who is online or not.
The background service is running since several days. The OS says nothing and doesn't kill nothing. Also I don't appreciate any increase in power consumption. There must be for sure if the connection is kept alive 24h24, but I don't note it.
Hope I can consider this a reliable solution. Anyway I put the user in the condition to switch back to FireBase notifications if he experience troubles.
Thank you


Glad to help
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Anyway: did you use a timer as in my sample to make periodic http request to your server ? Which interval did you choose ?
Or is your solution similar to the websocket sample that linked Erel ?

That was the question if you want to answer now :)
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Instant notifications on newer android versions are a crapshoot. With 8+ ive had nothing but issues. Even weather apps cant get severe warnings to me in time, sometimes I wont get the ping until hours later.

Even gmail itself is buggy. when a new email arrives, I may never get that notification. Then when I wake the phone up to look at it, put it back to sleep, bing. THEN the notification finally arrives.

Its insane. So join the club (This is a google pixel unmodified)
 
Upvote 0
Top