Service stops when phone is turned off

rfresh

Well-Known Member
Licensed User
Longtime User
I have a Service module in my app. It downloads a text file from my website every 15 minutes via FTP. However, when I press the power button on my phone and turn it off, the Service module stops working (I don't get my download status emails any more so I know it has stopped working).

The app is still in memory (I think) but should the Service module stop working when phone is turned off? Or is something else happening that I'm not aware of?

Thanks...
 

rfresh

Well-Known Member
Licensed User
Longtime User
Yes, I'm using ServiceStartAt() and yes, if I Force Stop the app the Service module stops too.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
OK, I will do that and see what it shows.

But my basic question is: when I turn off my Droid2 phone, is the Service module supposed to go to sleep as well? I thought it would continue to run in the background.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
But my basic question is: when I turn off my Droid2 phone, is the Service module supposed to go to sleep as well? I thought it would continue to run in the background.
It should keep running in the background.
Now unless you post some code, its all a guessing game.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Not all devices have the same behaviour when in sleep mode...

My Advent Vega tablet for example runs no background tasks while in sleep mode.
With the stock firmware the wireless driver is unloaded in sleep mode so even if a task was to run, if it required internet access it'd fail.
Custom ROMs have fixed that so the wireless driver remains loaded in sleep mode but still it runs no tasks in sleep mode.
(It has no 3G connection - wireless only like so many tablets).

But i think that a mobile can be expected to run tasks while in sleep mode.

Martin.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Hmmm...I added a Textwriter to my Service module to see what the time stamp was when the app wasn't actively running. The time stamp was not being updated. Below is my StartServiceAt() code.

Because thedesolatesoul suggested I write to a file just to see if the service module was running, I was surprised to see that just exiting to the phone home screen caused the file not to be updated every 30 secs. So, my code below, which is pretty simple, won't run unless the app has the focus. If I close the app (I press the Home key) and display my home screen, it won't run and if I press the power off button quickly to turn off the screen, it won't run. Note that I do not press and hold the power off key to fully power it off, just a short press to black out the screen.

So, I suspect my code below is setup correctly?


B4X:
'Main Activity
Sub Activity_Resume
   'start Service module
   StartServiceAt("FTP",DateTime.Now,True)
End Sub


'FTP Service Module
Sub Service_Start (StartingIntent As Intent)
   'write date time stamp to file
   StartServiceAt("",DateTime.Now + 30 * 1000,True)
End Sub
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I'll try that Erel...thanks...I have two phones, a Droid2 and Droid3 and they both have the same behavior...
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
OK, so I added .PartialLock in my code structure as shown below. However, when I turn the phone off, the Service module still goes to sleep.

Does anyone have any other ideas that I can try to keep my Service module working when the phone is off/asleep?

B4X:
'Main Activity
Sub Activity_Resume
    'start Service module
    StartServiceAt("FTP",DateTime.Now,True)
End Sub


'FTP Service Module
Sub Service_Start (StartingIntent As Intent)
    'send myself an email
    StartServiceAt("",DateTime.Now + 30 * 1000,True)
    pWakelock.PartialLock '<--- new code added here
End Sub
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Main Activity
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    Write_To_Dat_File 'save user settings
End Sub
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Okay, with the information you have provided it is impossible to say.

Clearly, when you press the home button, you are losing the scheduling of your service. Either StartServiceAt is being called again or some other similar command.

Without seeing the whole code it will be impossible to debug.

If you want to continue debugging, you will have to add more Log commands to see when the service dies (in Service_Destroy).
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Thanks for the comments.

>Clearly, when you press the home button, you are losing the scheduling
>of your service.

Actually, it continues to run ok when I press the home key and display my home screen again. It stops when I press the power off button (quick press, not the long press which powers down the whole phone).

I will add more Log commands in the Main pause event and see what is going on. Thanks again...
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
So I downloaded aLogCat app to see my logs and here is what I've discovered is going on with my Droid2 phone.

My Service FTP's down a small text file and emails it to me on a 10 minute intrerval. I discovered the SMTP object was producing an error (didn't know why at the time) so I just remm'd out the SMTP code and decided to just let the log run.

Now, without the SMTP causing an error, what I discovered was that on the 2nd 10 min check, there was a SocketException, specifically a timeout when trying to connect to my website. On the next (3rd) 10 min connect attempt there was a SocketException Broken pipe error.

During this time, I am on my website with a page that refreshes automatically every 5 minutes and the site is up. So, I'm now thinking there is an internet connection issue on my Droid2 phone. It's on my home wifi signal and I'm using that all the time on my PC while my driod2 has been turned off and sitting on my table trying to connect every 10 minutes and failing to do so on the 2nd 10 min check.

Recall I turn off my phone by quickly pressing the power button until the screen goes off.

I know this isn't much to go on, but any thoughts appreciated as to what I can try next?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Look at your device's WiFi sleep policy:

Settings > Wireless & network > WiFi settings.

Now click Menu key and select Advanced.

Look under WiFi sleep policy - you might need to enable the Never sleep setting.

That could and probably will cause a slight increase in power consumption - whether it's noticeable or not you'll have to see.

Without making any changes to that setting - does your device check emails while in sleep mode?
Ie do other programs have internet access while the device is asleep?

Martin.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
@warwound

You're a genius...that was the problem. My wifi setting on my Droid2 was set to turn off when the screen goes blank. Excellent analysis on your part. It ran ok past the 2nd and 3rd 10 interval FTP download. I'm letting it run and waiting for the 4th 10 minute interval now.

OK, so this begs a couple of questions:

1. Can use code and set the wifi never sleep setting? Or is this a security issue and we can't do this using code?

2. Can I detect if I have a wifi connection that is 'active' vs 'asleep'?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
1. Can use code and set the wifi never sleep setting? Or is this a security issue and we can't do this using code?

It looks at though you can change that setting on the fly with java - so a new library would be required or does an existing library offer such functionality?

Here's the documentation for the method required: Settings.System | Android Developers.

And some example code here: https://groups.google.com/group/and...aa7e981888?hl=en&lnk=gst&q=WIFI_SLEEP_POLICY#.

Note that you'd need to update your manifest file to give the app permission to change that setting.

I've had a search of the forum and found no mention of a library that can do this - if you search and cannot find anything i could probably create such a library.

2. Can I detect if I have a wifi connection that is 'active' vs 'asleep'?

Maybe Wifi Manager Library?

Otherwise i could incorporate it into the library to change the sleep policy.

I'll be busy on and off during the week so can't promise an immediate new library but will find time at some point if a new library is required.

You could probably use the Reflection library to change this setting instead of creating another new library but i'm not sure what syntax you'd need.

Martin.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Yes, I think the WiFi Lib is the answer here...thanks again...4 gold stars for you tonite...!!!
 
Upvote 0
Top