Android Question How to create a program that runs even while device is sleeping.

GaryK4

Member
Licensed User
Longtime User
I need to run a program that runs everyday at around 6:00am even if the phone is sleeping.
It FTP's a text file from a server. Then reads the file and updates a SQLite database.
I put this code in the main (non server) application and use Tasker to start the program every morning. This only works some times. It starts the program, but it seems to be in the background. That is why I think some kind of background server program may work better.
I don't know where to begin with this.
 

drgottjr

Expert
Licensed User
Longtime User
regarding tasker, it's unclear exactly what you mean by:
This only works some times.
i would suspect the network connection is down because the device is sleeping. if tasker wakes your
app at 6 and your app immediately tries to download a file, there will be no network connection. if your
app doesn't wait a little and try again, there will be no download. i'm guessing tasker's job is to wake the
device at 6 and to hand things off to it, and that's that. i have no idea how your app goes about doing
what it's supposed to do.

i think you will find it easier to have a look at your app and to keep using tasker than to try to handle this on
your own. tasker seems to have mastered what could take you a long time to get a handle on. starting
actions at a given time is proving very constrained with each passing version of android.
 
Upvote 0

GaryK4

Member
Licensed User
Longtime User
Only works sometimes: At times the program connects to FTP, other times it fails. So, tasker has started the app.
You may be correct that the network connection is down. I just added a task to turn on WiFi before starting my app.
In my app, I added a loop to retry the FTP every 5 seconds for 5 times. The results goes into a transaction log.
Thanks
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
if a network connection was live before the device went to sleep, it will be
re-established upon awakening. at least, the os will try. it's a matter of time
(over which the device has no control).
every 5 seconds 5 times is like pounding at someone's door over and over
without giving them a chance to get to the door. it also has the affect of
tearing down what was started. the device was already trying to make a
network connection (among other thing) without your hounding it.
 
Upvote 0

GaryK4

Member
Licensed User
Longtime User
If the tasker profile that I added works, then my program will connect to FTP and exit the loop. My 5 time is a crude way to do a wait for.
I don't know how long to wait for a retry. I can play with these parameters. The log will tell me which attempt worked.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…