Android Question Using StartServiceAt as a Timer ?

lemonisdead

Well-Known Member
Licensed User
Longtime User
You shouldn't use ServiceStartAt as a timer. You should instead use a real timer.

Original thread

@lemonisdead I personally use StartServiceAt set to True and as a Sticky Service in all my widgets and services. I use StartServiceAt as a timer (Mainly set to every 60 seconds or 60 minutes (for my weather app)) as I find that using an actual timer is not reliable enough for me, especially for my clock app/widget.

Resources wise I've just check settings - battery usage on 4 devices running my widgets and services and not one of my widgets or services are showing up in the standard Android battery usage list :) lowest is 1%.

I will use a timer within StartServiceAt if what I'm doing with the timer falls within 60 seconds of the StartServiceAt starting...

Hello @Peter Simpson,

Thanks for your reply and the example you provided. I think that this example is not too much energy consuming because the widgets are handled by the system.

But what about another example : you have to check some information twice a hour.

Option 1 :
  • create a sticky service including the timer
  • modify the PhoneWakeState to KeepPartialLock
Option 2 :
  • create a service in charge of starting a second service which will collect the information
  • use StartServiceAt with During Sleep at True so you don't have to worry about the wake state
I do think that option 2 is the way to go. Or, am I wrong ? (from your experience, I am not too wrong ;)

Many thanks
 

Peter Simpson

Expert
Licensed User
Longtime User
My weather app collects the JSON weather feed from the www every 60 minutes. It also used to collect a seperate XML for Sunrise and Sunset, but a few weeks ago I found the Astro library for B4A so I no longer collect the XML feed, but I still fetch the JSON weather feed every 60 minutes on the hour exactly.

As I've mentioned previously, my widgets are not listed in the standard Android battery usage list so I'm happy about that. I also have an auto IP address updater service that contacts an online server and tells it what my current IP address is, at the moment that is set to every 30 minutes (it used to be every 60 minutes), that service is also not on my battery usage list.

My solution is a mix between your two options, I use a sticky service with StartServiceAt during sleep set to True and that's all. My widgets and services just runs and runs and runs. I also always check for an actual internet connection before executing any HTTP calls to the internet, I test for the internet by using MLWiFi. If MLWiFi says that there's no internet I try again 7 seconds later :)

I'm sure @Erel or somebody else will say that my solution is the incorrect way to go but I've not came across any issues with my widgets or services not starting every 60 seconds or 60 to 90 minutes. So in my case if it ain't broke, don't fix it :p

I've not tried my service set to every 12, 18 or 24 hours, but it does work set to every 6 hours on all my devices. I have almost 3k using my IP address app, nobody has complained about it not updating the online server. The service provider recommends that users of my app set it to update their servers every 15 minutes, mine is set to 30 minutes, it was set to every 60 minutes at one stage with no issues whatsoever.

I personally refuse to use PhoneWakeState in any of my applications @lemonisdead. I absolutely hate waking up in the morning and checking my battery usage in setting and seeing that my simple widgets or services have taken up 5% of my battery in just 6 hours :mad:, PhoneWakeState is not for me, even when using it like a switch flicking PWS On and Off.

I personally do not use timers for constantly long running tasks...
 
Last edited:
Upvote 0
Top