Android Question Service reliably once a day

Nickelgrass

Active Member
Licensed User
Longtime User
Hello,
fist of all happy christmas to all!

I am currently rotating around a little "problem".

I have an App with a service. This service needs reliably start once very 24 hours to do something (time of day is irrelevant just every 24 hours). I selected the StartAtBoot. And in the Main Activity_create I added the line in

If FirstTime AND IsPaused("some_service") Then StartService("some_service")

and in the service_start of the service

StartServiceAt("some_service", DateTime.Now + DateTime.TicksPerday, True)
and then some code.

Problem is now it fires almost every time the main activity is started. This behavior is normal for the app gets killed by the system and is created new.

Now I thought about putting If StartingIntent.ExtrasToString.IndexOf("ALARM_COUNT") = -1 Then Return in the service_start in front of the startserviceat.

My concern is now if for some reason the scheduled service is canceled then the code will never be executed. The question is now: how reliably will the service run every 24 hours if I remove the code from the activity_create? Is it enough to set start at boot and then reschedule the service? Would there not be any way to get a list of scheduled services and just make sure?

Thanks!

Regards
 

Nickelgrass

Active Member
Licensed User
Longtime User
After doing a bit searching I found that the Alarmmanager could return the schedule. For this one would have to create an exact same alarm again and retrieve its schedule. That means basically it is possible to get the schedule of one specific alarm. Are the StartServiceAt scheduled using the Alarmmanager? If so it would be possible to do something like a GetServiceSchedule("service_name"). Is there any info on how the Services are scheduled in B4A?
 
Upvote 0

Nickelgrass

Active Member
Licensed User
Longtime User
Thanks for the reply. It just seems a bit cleaner to actually test if the service is scheduled instead of starting it over again and letting it decide. Is there any documentation on how the AlarmManager is used in B4A with the service name or is there any way to look at the generated code? I would like to try and make a lib to get the scheduled time of a specific service.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
public static void StartServiceAt(BA mine, Object Service, long Time, boolean DuringSleep) throws ClassNotFoundException {
AlarmManager am = (AlarmManager) BA.applicationContext.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(mine.context, 1, getComponentIntent(mine, Service),
PendingIntent.FLAG_UPDATE_CURRENT);
am.set(DuringSleep ? AlarmManager.RTC_WAKEUP : AlarmManager.RTC, Time, pi);
}
 
Upvote 0

Nickelgrass

Active Member
Licensed User
Longtime User
Great, thanks Erel! I will see if it works and post the result. I suppose "Service" in StartServiceAt(BA mine, Object Service... and getComponentIntent(mine, Service) needs to be substituted with the service module name?
 
Upvote 0
Top