4

Envrionment: Eclipse
Language: Java (Android)

I have a bit of a problem, which I didn't realise until I tested my application out on a device. I always thought that services would continuously be running in the background, even when the phone's sleeping. I found out that this is not the case, so, my question is that does the service start up again once you wake your device up? And if not, how would I cause the service to start-up again.

Would I be able to wake the phone every 5 minutes or so, just to run my service, which will last 30 seconds to 1 minute. And then make the phone sleep again?

Thanks in advance.

EDIT: I am very new to Android programming and would really appreciate if someone would tell me how to use WakefulIntentService. I have a service that is searching for the user's GPS Location every so often, and when the phone goes to sleep, I want my service to still look for their location. How would I go about using the WakefulIntentService for this? And would I be able to use it in this scenario.

Thanks.

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
user959631
  • 1,004
  • 2
  • 14
  • 34

4 Answers4

11

You need to hold the processor lock to keep your service running

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
wl.acquire();
// When you are done
wl.release();

And if your service is using Wi-Fi, you need another lock as well

WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiLock= wm.createWifiLock(WifiManager.WIFI_MODE_FULL, "TAG");
wifiLock.acquire();

// When you are done
wifiLock.release();

Remember to add android.permission.WAKE_LOCK in your manifest

iTech
  • 18,192
  • 4
  • 57
  • 80
  • I forgot to mention that I am quite a noob at Android development, as you probably have figured out, from my above question, but could you please elaborate what you mean by `lock`. Thanks for the speedy reply... – user959631 Feb 15 '13 at 23:48
  • 2
    The above approach will kill the battery quickly. – 323go Feb 15 '13 at 23:49
  • 1
    The question is **not** about battery, and if you design you app *correctly* it should not affect – iTech Feb 15 '13 at 23:52
  • Thanks guys, @323go I know that the battery life will be drained quickly, but I don't need to be running the above code 24/7 it's only certain times in the day, which last up to a maximum of 15 minutes. Would that still affect badly? Also I will test it out and see how badly it drains, and if it kills it very quickly, I shall use a different approach. Thanks again guys... – user959631 Feb 16 '13 at 00:01
  • @iTech, setting a wifi lock and a wake lock to keep the service running in the background is a bad idea, unless the service is actually doing something. Consider that the question wasn't about the battery because the asker didn't know the implications -- by ignoring those, you pointed him to the wrong answer. user - the correct answer is to look at a WakefulIntentService. – 323go Feb 16 '13 at 00:14
  • Thanks @323go, the service is doing something, it's looking for your GPS location or looking for Wi-Fi SSID's in range... – user959631 Feb 16 '13 at 00:23
  • @323go using `lock` is definitely needed in so many cases and if you use it *correctly*, it should not affect the battery life! open android source code and the system apps developed by Google and you will find it is using `lock` a lot! saying "the correct answer is to look at a WakefulIntentService" as general statement is indeed incorrect – iTech Feb 16 '13 at 00:26
  • "it should not affect battery life" is a false statement. Of course it will ALWAYS affect battery life if used. The idea is to reduce the effect as much as possible. Further, please look at the question. It wasn't how to keep the service working, but how to wake it up after it was stopped. – 323go Feb 16 '13 at 01:12
  • ....using the device will "waste" battery life. If "locks" are used when they are needed, and released when they are not, using them should be OK. They're there for a reason. – dell116 May 11 '13 at 00:33
  • dont worry about the battery....let the code do its task....above code works for 3G perfectly. – dd619 Jan 21 '15 at 08:41
3

I have a service that is searching for the user's GPS Location every so often, and when the phone goes to sleep, I want my service to still look for their location. How would I go about using the WakefulIntentService for this? And would I be able to use it in this scenario.

WakefulIntentService is inappropriate here, as it is designed only to keep the device awake for a short period of time to do some work. It is not designed to keep the device awake for an indeterminate time, such as the time it takes to get a GPS fix.

I have a LocationPoller project that handles your scenario a bit better.

I am very new to Android programming

What you are trying to do is not a suitable project for somebody with your Android experience level, IMHO.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thanks, I appreciate your help and opinions. I guess it's not suitable, but that's the way how I learn. I set myself a task to do. And see if I can accomplish that task. If I get stuck along the way, I learn via searching Google and asking questions here at StackOverflow. By the way, I found out another way, I used `AlarmManager`, I looked at your `WakefulIntentService` and that gave me an idea on what to do. Thanks again for your help. – user959631 Feb 25 '13 at 18:52
0

I don't know what your service is doing exactly, but if it's down/uploading data to a server you might want to consider using a Sync Adapter.

Unless you need exact timing this is less of a burden on your system than AlarmManager.

I you are doing something else, you might want to look into the JobScheduler API.

Anyway if you really want to stick to AlarmManager just know that Inexact Repeating is already a lot better than Exact Repeating.

LanderV
  • 1,159
  • 12
  • 14
  • Thanks for a reply, however, this question was asked two years ago (by me) and already has an accepted answer (two years ago) Please try to avoid reviving old threads, thanks ;) – user959631 Sep 18 '15 at 09:17
  • 2
    Other people will still find this thread looking for an answer to a similar problem. That's why I replied. But other than that you are right. – LanderV Sep 22 '15 at 14:40
-6

I used AlarmManager, I looked at WakefulIntentService and that gave me an idea on what to do. Thanks again for the help everyone.

user959631
  • 1,004
  • 2
  • 14
  • 34