Android Question if Startserviceat fails...

renzino

Member
Licensed User
Longtime User
Hi, I need a method to be sure that my service starts really. after hundreds of successful tests, it's happened (only one time, but it's happened!) that the service didn't started even if the app was still running. i didn't undestood the reason, but for the future i would prevent this trouble. determining if the service didn't started after an amount of seconds, and in this case taking some action (for example forcing stop and restart of the service). thanks a lot
 

Peter Simpson

Expert
Licensed User
Longtime User
I take it that there are no activity modules?
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
What if you have a global variable as a flag for a service

Every time the service starts the flag is raised, When the services finishes the flag is lowered.

At any time you can check the flag. If the flag is raised, then the service has started and it's running.
 
Upvote 0

renzino

Member
Licensed User
Longtime User
well, i have two modules, Main and My_Service. In Main module i put "StartServiceAt(My_service, My_Time, True)". i would like to know if, at a certain time after "My_time", the module "My_service" is running and, if not, i have to launch "manually" the module "My service". Perhaps it's simplier than i think but....
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I've never seen a StartServiceAt not run when requested to do so, so I can't comment about that. try the following out.

First make sure that you have the service as a Sticky Service.
Try this https://www.b4x.com/android/forum/t...-long-running-background-tasks.27065/#content

If you still have concerns then you could always add a timer to your main activity and then use the following line in the timer tick sub.
B4X:
If IsPaused(ServiceName) = True Then StartServiceAt(ServiceName, Whatevertime, True)
You don't even need the above line in a timer, you could just place the above line into Activity_Resume.

Enjoy...
 
Last edited:
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Peter's response is better than mine, but the principle is the same. You don't actualy need a flag, as B4A already includes that flag :)
 
Upvote 0

renzino

Member
Licensed User
Longtime User
thanks to all of you, I put the code suggested by Peter, I hope my app will start for ever without any problem!
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
@renzino do not forget to have your service running as a sticky service...
B4X:
#Region  Service Attributes 
    #StartAtBoot: False
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region
 
Upvote 0
Top