Android Question 2 Background Services

Jmu5667

Well-Known Member
Licensed User
Hello

Can we have 2 background services, examples below:

B4X:
/ *** SERVICE #1

Sub Service_Start (StartingIntent As Intent)
  
    ' // setup the notification  
   Service.StopAutomaticForeground
   Service.StartForeground(nID,CreateNotification("Title","text","logo",Main,False,False))
   pw.PartialLock


End Sub


Private Sub CreateNotification(Title As String, Content As String, Icon As String, TargetActivity As Object, Sound As Boolean, Vibrate As Boolean) As Notification
  
 
   Dim p As Phone
  
   If n.IsInitialized Then
       n.Cancel(nID)
   End If
  
   If p.SdkVersion >= 21 Then
       Dim nb As NotificationBuilder
       Dim Channel As NotificationChannelBuilder
       Dim ChannelID As String = "ies_atlas_hello"
       Dim ChannelName As String = "Atlas Hello"
      
       ' // Build a Channel
       If Channel.ChannelsSupported Then
           Channel.Initialize(ChannelID, ChannelName, Channel.IMPORTANCE_LOW)
           Channel.Build
       End If
      
       nb.Initialize(ChannelID)
       nb.AutoCancel = False
       nb.OnGoingEvent = True
       nb.DefaultSound = False ' sound
       nb.DefaultVibrate = False ' vibrate
       nb.DefaultLight = False
       nb.ContentTitle = Title
       nb.ContentText = Content
       nb.setActivity(TargetActivity)
       nb.SmallIcon = Icon
       nb.ShowTime = False
       nb.Number = nID
       ' //set the return object for the notification object (n1) in this service
       n = nb.GetNotification
   Else
       n.Initialize
       n.Number = nID
       n.Light = False
       n.Vibrate = False
       n.Sound = False
       n.OnGoingEvent = True
       n.Icon = Icon
       n.SetInfo(Title, Content, TargetActivity)
   End If
   n.Notify(nID)
   Return n
  
End Sub

/ *** SERVICE #2

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground
   ' // hook to the forground notification in the service #1
   Service.StartForeground(service1.nID, service1.n)
   pw.PartialLock
  
End Sub
I have primary service(service 1) that manages necessary events for the app, this must be running. The secondary service (service 2) is for messaging (Whatsapp styled), connects to a B4J server which allows the sending and receiving of messages.

The issue is really on android 8+ where the second service seems to paused by the device (hauwei Y6 Oreo) as I have a vibrate every 30 seconds to let me know the service is still running, it does maybe 4-5 vibrates then sometime later the device will vibrate for 10 seconds or so (continously as if the events were queued)


This all happens when the screen if off and the app is not visible.

All help appreciated

Regards

John
 

OliverA

Expert
Licensed User
After reading a previous post of yours (https://www.b4x.com/android/forum/t...ps-when-screen-turned-off.103249/#post-647187) and @Erel's answer(https://www.b4x.com/android/forum/t...ps-when-screen-turned-off.103249/#post-647189), it would seem that you can start the second service (not a foreground service) from the first foreground service, then the second service would inherit the "foreground serviceness" of the first service. Either that, or I misunderstood your question and @Erel's answer.
 

Jmu5667

Well-Known Member
Licensed User
After reading a previous post of yours (https://www.b4x.com/android/forum/t...ps-when-screen-turned-off.103249/#post-647187) and @Erel's answer(https://www.b4x.com/android/forum/t...ps-when-screen-turned-off.103249/#post-647189), it would seem that you can start the second service (not a foreground service) from the first foreground service, then the second service would inherit the "foreground serviceness" of the first service. Either that, or I misunderstood your question and @Erel's answer.
Ok, I will make sure that the second is started from the first service, and post the results.
 
Top