Android Question #StartAtBoot: True not working

Duck

Member
Hello,

In my little test app I added a service module with the line #StartAtBoot: True
After new boot the service did not start.

The program catches a screen unlock and shows toastmessage "unlocked"
It should work in the background to do that until the end of times.
What am I doing wrong?

Thanks for helping me out.

My code:
Service1:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

Sub Process_Globals
    Private PE As PhoneEvents
    Dim Notification As Notification
End Sub

Sub Service_Create
    Service.StartForeground(1,CreateNotification("..."))
    PE.Initialize("PE")
    Notification.Initialize
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground
End Sub

Sub PE_ScreenOn (Intent As Intent)
    ToastMessageShow("Unlocked",False)
End Sub

Sub CreateNotification (Body As String) As Notification
    Dim Notification As Notification
    Notification.Initialize2(Notification.IMPORTANCE_LOW)
    Notification.Icon = "icon"
    Notification.SetInfo("TestApp", Body, Main)
    Return Notification
End Sub
 

Duck

Member
Check the logs to see if it started or not.

Check this example: https://www.b4x.com/android/forum/threads/background-location-tracking.99873/#content

Don't assume that Android won't kill the app at some point.
Do you mean the logs window(F6)? Nothing is happening there. I have #BridgeLogger: True in Main module.
I also added 'log("service started")' in Service_Start sub, which doesn't show in log on reboot.
Will the b4a bridge be ready on time after reboot to communicate to the log window?
Or are other other log files?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Uncheck the "Filter" box on the logs tab, wait until the stream of device events settles down a bit, then try to start your app and then immediately disconnect your device from the IDE so it will limit the amount of unfiltered events in the list to just those around the time when you attempted to start your app.

Then find the lines that give a clue to whats going wrong.
 
Upvote 0

Duck

Member
Uncheck the "Filter" box on the logs tab, wait until the stream of device events settles down a bit, then try to start your app and then immediately disconnect your device from the IDE so it will limit the amount of unfiltered events in the list to just those around the time when you attempted to start your app.

Then find the lines that give a clue to whats going wrong.
I unchecked the filter box.
After reboot it takes 1 minute and then to the log file is added (from my log command) 'service started'
However there is no notification send nor is the 'PE_ScreenOn' triggered if i unlock the screen again.
After another minute the 'Service_Destroy' sub is triggered and the command log("service destroyed") I have added shows in the log.
(My phone is a Oneplus 6T, android 11)
 

Attachments

  • log.txt
    2.9 KB · Views: 152
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Service.StopAutomaticForeground
You're stopping the service as soon as it starts. Therefore you may not get any PE notifications.

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

Sub Process_Globals
    Private PE As PhoneEvents
    Dim Notification As Notification
End Sub

Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    PE.Initialize("PE")
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(1,CreateNotification("..."))
End Sub

Sub PE_ScreenOn (Intent As Intent)
    Log("Screen turned on")
    ToastMessageShow("Unlocked",False)
End Sub

Sub CreateNotification (Body As String) As Notification
    Dim Notification As Notification
    Notification.Initialize2(Notification.IMPORTANCE_LOW)
    Notification.Icon = "icon"
    Notification.SetInfo("TestApp", Body, Main)
    Return Notification
End Sub
 
Upvote 0

Duck

Member
You're stopping the service as soon as it starts. Therefore you may not get any PE notifications.

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

Sub Process_Globals
    Private PE As PhoneEvents
    Dim Notification As Notification
End Sub

Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    PE.Initialize("PE")
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(1,CreateNotification("..."))
End Sub

Sub PE_ScreenOn (Intent As Intent)
    Log("Screen turned on")
    ToastMessageShow("Unlocked",False)
End Sub

Sub CreateNotification (Body As String) As Notification
    Dim Notification As Notification
    Notification.Initialize2(Notification.IMPORTANCE_LOW)
    Notification.Icon = "icon"
    Notification.SetInfo("TestApp", Body, Main)
    Return Notification
End Sub
 
Upvote 0

Duck

Member
Thanks guys,

I took Erel's code from his 'background-location-tracking' link.
There I added my PE_Screenon code and the corrected line: Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER.
Sofar so good. It starts on boot, staying alive (for a few days now).
 
Upvote 0
Top