Android Question Why does this service not start at bootup?

biometrics

Active Member
Licensed User
A service which starts at bootup with "#StartAtBoot: True" isn't working on Android 7.1.2. It works fine on version 4-6.

I created a new app & service with nothing but a toast message when the service starts. It's not starting on boot up. Can you see what is wrong?

I'm using B4A 9.5.

I've attached the app source zip but here is the code too:

Main Activity

B4X:
#Region  Project Attributes
   #ApplicationLabel: Service Test
   #VersionCode: 1
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   StartService(TestService)
   Activity.Finish
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Starter Service
B4X:
#Region  Service Attributes
   #StartAtBoot: False
   #ExcludeFromLibrary: True
#End Region

Sub Process_Globals

End Sub

Sub Service_Create

End Sub

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

Sub Service_TaskRemoved
   
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

Sub Service_Destroy

End Sub

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

Sub Process_Globals

End Sub

Sub Service_Create
   ToastMessageShow("Service_Create", True)
End Sub

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

Sub Service_Destroy

End Sub

Manifest

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
 

Attachments

  • ServiceTest.zip
    8.1 KB · Views: 88

Erel

Administrator
Staff member
Licensed User
The code looks correct though the service will be killed quite quickly if it is not in foreground mode.

1. Are you monitoring the logs while the device is booted? It is possible that the toast message is lost for some reason.

2. Make sure that your device doesn't have a "fast boot" setting as it can cause apps not to start after boot.
 
Upvote 0

biometrics

Active Member
Licensed User
The code looks correct though the service will be killed quite quickly if it is not in foreground mode.

1. Are you monitoring the logs while the device is booted? It is possible that the toast message is lost for some reason.

2. Make sure that your device doesn't have a "fast boot" setting as it can cause apps not to start after boot.
I had a look at this thread but it seems to be about Android 8+ and my problem is with Android 7+ (https://www.b4x.com/android/forum/threads/automatic-foreground-mode.90546/).

I added a timer to do a toastmessage every five seconds. It doesn't show. So the service isn't starting. Quite honestly I have no idea why or how to fix it. How do you set a service to foreground mode, I tried the following but it's not working:

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

Sub Process_Globals
   Dim libTimer As Timer
   Dim libPhoneWakeState As PhoneWakeState
End Sub

Sub Service_Create
   ToastMessageShow("Service_Create", True)
   
   libPhoneWakeState.KeepAlive(True)
   libPhoneWakeState.PartialLock

   Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
   
   libTimer.Initialize("libTimer", 5000)
   libTimer.Enabled = True
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub libTimer_Tick
   ToastMessageShow("libTimer_Tick", False)
End Sub
 
Upvote 0
Top