Android Question Advice about Services

lentunti

Member
Licensed User
Longtime User
Hi All,

I have looked at the forums regarding services but I am a little confused, especially with the changes with Android 8.

What I am trying to accomplish to create a service that runs in the background (and has to start on boot) and scans for BLE beacons every 5 minutes. I am happy with the beacon part and have written the software using a sticky service and a timer to scan the beacons (running on Android 4 to 6).

The problem is the service gets destroyed and restarts 10 to 20 minutes later.

Could someone please tell me what type of service I should use please?

Thanks for any help.

Len
 

MarkusR

Well-Known Member
Licensed User
Longtime User
i used it like this for a permament mqtt connection to my alarm system raspberry pi server.
and because of power consume i can start/stop it at needing.

the starter service i set to #StartAtBoot: False
and if i start the app i started my service too or it would only start at boot.
Starter Service
B4X:
Sub Service_Start(StartingIntent As Intent)
            
    StartService(Connection)
    
End Sub

Connection Service
B4X:
#Region  Service Attributes
   #StartAtBoot: True
  
#End Region

Sub Process_Globals
   Dim PhoneWakeState1 As PhoneWakeState
End Sub

Sub Service_Create
  
End Sub

Sub Service_Start(StartingIntent As Intent)
       
    'Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    Dim n As Notification
    n.Initialize
    n.Icon="icon"
    n.Sound=False
    n.Light = False
    n.Vibrate = False
    n.SetInfo("Alarm","Listening for a Alarm Message",Main)
    Service.StartForeground(1,n)

    PhoneWakeState1.PartialLock
   
End Sub

Sub Service_TaskRemoved

    PhoneWakeState1.ReleasePartialLock

End Sub

Sub Service_Destroy

    PhoneWakeState1.ReleasePartialLock

End Sub
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Hi All,

I have looked at the forums regarding services but I am a little confused, especially with the changes with Android 8.

What I am trying to accomplish to create a service that runs in the background (and has to start on boot) and scans for BLE beacons every 5 minutes. I am happy with the beacon part and have written the software using a sticky service and a timer to scan the beacons (running on Android 4 to 6).

The problem is the service gets destroyed and restarts 10 to 20 minutes later.

Could someone please tell me what type of service I should use please?

Thanks for any help.

Len
I have been struggling with the same issues for some time now, what i have found out is that for a service the minimum starting interval is 15 minutes, so if you're trying to re-start a service every minutes it won't work, the only way from what I understand to keep a service running without the OS destroying the service is by running your service in foreground mode which requires a notification icon to be displayed.

Kind of annoying if you ask me.

Walter
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
the service is by running your service in foreground mode which requires a notification icon to be displayed.
at least u see its running :) and it also open the app activity if u click this notification.
the whole android system is a worst thing for developers ... it consists of endless problems for us.
i still waiting for a cheap linux phone that i can use the same app everywhere.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
at least u see its running :) and it also open the app activity if u click this notification.
the whole android system is a worst thing for developers ... it consists of endless problems for us.
i still waiting for a cheap linux phone that i can use the same app everywhere.
That is true, but believe me when I tell you that if you are building an app where you require to a background service to run and if the only way is by displaying a notification icon, customers will hate it, believe me i've dealt with that before.

Walter
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
myself did not like if apps using hidden background services. i like to start the app and it runs and if me end this it stops. same as desktop exe.
the annoying thing is that people try to drag out this notification label and it can not removed.
 
Upvote 0
Top