Service - How to introduce delay in startup

sangee

Member
Licensed User
Longtime User
Hello All...

Is there a we can introduce some time delay in starting of a service module which is set to start on boot?

I have a service which reads a text file with the stored values located in the SD card (dir.external). I have noticed that every time the phone boots the service just crashes as it tries to access that file for information. I tried commenting the file read part and service just starts good.

Can any one please help me on some way to introduce a delayed start to the services. Basically i want my services app to start when the boot is complete and storage is available to access...

Thanks in advance... :)

- Sangee
 

edgar_ortiz

Active Member
Licensed User
Longtime User
Try schedule the service

B4X:
'
If Main.FirstTime = True Then
   '
   StartServiceAt("", DateTime.Now + 2 * DateTime.TicksPerMinute, False)
Else
   '
   ' Continue the normal process
   ...
   ...
End If

Regards

Edgar
 
Upvote 0

sangee

Member
Licensed User
Longtime User
But the first start up is the issue as the service tries to start before the SD card is mounted and is ready for use. There must be a way to delay the startup of services which are set to start on boot..

Thanks,
Sangee
 
Upvote 0

MotoMusher

Active Member
Licensed User
Longtime User
Try this

in globals
B4X:
Dim Timer1 As Timer

in activity_create
B4X:
Timer1.Initialize("Timer1", 3000)  ' 3 seconds in ms  
 Timer1.Enabled = True

Create a sub
B4X:
Sub Timer1_Tick
timer1.enabled = false
If IsPaused("NameOfYourService") = True Then 'not started
   StartService("NameOfYourService")
End If

end sub

That timer1_tick puts a 3 second delay on the code you want to run. You can test if the card is mounted etc as well in there and restart the timer the same as you did in service_create if it is not ready yet.
 
Upvote 0
Top