Android Question Automatic Foreground v.s Persistent

ac9ts

Active Member
Licensed User
Longtime User
I have an app that uses a service to stream MP3 files. Some users would report that the stream would stop when running in the background so I added an option where they could run the app in persistent mode using this:

B4X:
Sub Service_Start (StartingIntent As Intent)

    If GUI.PersistentPlayer Then
       
        If Not(Notification1.IsInitialized) Then
            Notification1.Initialize
            Notification1.Insistent=False
            Notification1.Light = False
            Notification1.Sound = False
            Notification1.Vibrate = False
            Notification1.OnGoingEvent=True
            Notification1.AutoCancel=False
        End If
   
        Service.StartForeground(NotificationID, Notification1)

    End If
   
    GetLocks
   
End Sub

I am now using B4A v8.0 and Oreo users are seeing the service get killed so I added
B4X:
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
to Service_Create. This corrects the issue on Oreo. My question, is there an advantage of one over the other? The other reason I ask is that the persistent mode code above crashes when compiled on v8.0. I haven't looked into why yet but I seem to remember seeing something regarding the order of setting to various Notification settings.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Initialize the notification object each time.
2. You must call SetInfo or SetInfo2.

3. Both options are good. There is no real difference between them.

If you are not setting the automatic foreground mode to ALWAYS then you should make sure to call StopAutomaticForeground to avoid having two icons.
 
Upvote 0
Top