Android Question Using StartServiceAt as a Timer ?

Discussion in 'Android Questions' started by lemonisdead, Apr 19, 2015.

  1. lemonisdead

    lemonisdead Well-Known Member Licensed User

    Original thread

    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
     
  2. Peter Simpson

    Peter Simpson Expert Licensed 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: Apr 19, 2015
    lemonisdead likes this.
  3. Peter Simpson

    Peter Simpson Expert Licensed User

    How did you get on killed the lemon, oops I mean @lemonisdead? ;)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice