Android Question service - avoid execute service if already running

leitor79

Active Member
Licensed User
Longtime User
Hi,

I have a service which is set to execute every 30 seconds to perform a simple task, that is configured like this:

B4X:
Sub Service_Start (StartingIntent As Intent)

    Try
        StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerSecond, False)
        If StartingIntent.HasExtra("android.intent.extra.ALARM_COUNT") Then
        .
        .
        .

The question is... if, for some reason, the execution takes more than 30 seconds, will a second instance of the server be fired before the previous one finishes? In case of yes, which would be the best-practice approach to avoid that (ie: to fire the new start 30 seconds after the previous one finishes)?

Thank you very much!
 

leitor79

Active Member
Licensed User
Longtime User
Hi Erel,

Thank you for your answer.

I've thought about that... but, if the service is configured to startatboot, will it start anyway, although StartServiceAt is at the end?

Thank you!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
When your app is started at boot... Do your work and set a new startserviceat to shedule the next run and stop the service if you have done your work
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi, Don! Thank you for your answer!

And with what do I have to check the StartingIntent, besides ALARM_COUNT, to check if the service was automatically started at boot? Because I imagine that if I put the StartServiceAt at the end, the StartingIntent.HasExtra("android.intent.extra.ALARM_COUNT") will be "False" at first start?


Regards,
 
Upvote 0

Sgardy

Member
Licensed User
Longtime User
I solved this problem setting a global variable in my service (ImRunning = true), I check first Myservice.ImRunning before executing other. Do it's correct this approach?
 
Upvote 0

Sgardy

Member
Licensed User
Longtime User
Thank you Peter, I'm an absolute beginner I'll test it soon! Bye
 
Upvote 0
Top