17

I am bit confused after going through the questions & answers in Stackoverflow about WakefulIntentService. I just would like to get some knowledge on this topics to make sure my understanding is correct, please feel free to correct me, if I am wrong.

I built a small application, where I am using a background Service that keeps playing music whenever the user shakes the mobile. I tested after the device is locked and screen is turned off and it works as expected.

  1. What I am hearing from this forum, the service might turn off as soon the device goes to asleep. Is that true? In my case, it works always, Am I missing something?

  2. What is the need of WakeFulIntentService? When do we need to use WakefulIntentService?

  3. I tried running a timer in a Service, though the device is locked and screen is turned off and my timer is running pretty much I can say for sure. Because I used to get notification whenever my timer trips.

Community
  • 1
  • 1
Ramesh Sangili
  • 1,633
  • 3
  • 17
  • 31

2 Answers2

19

What I am hearing from this forum, the service might turn off as soon the device goes to asleep. Is that true?

Yes.

In my case, it works always

Then something else on your device is keeping the device from falling asleep. Perhaps use adb shell dumpsys power to see what WakeLocks are outstanding.

What is the need of WakeFulIntent Service? When do we need to use WakefulIntentService?

The device may fall asleep if the user is inactive and nothing is keeping the device awake. A WakeLock is used to ensure the device stays awake. For transactional-type work (e.g., downloading a file), WakefulIntentService combines an IntentService and a WakeLock to make keeping the device awake as long as necessary (and only as long as necessary) relatively easy.

WakefulIntentService is not suitable for use with services that need to run indefinitely, such as a music player. For those, manage your own WakeLock.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am spawning a thread in my service to keep my timer running always. I am scheduling the timer inside the run method of the thread? Whether thats the reason the timer is alive though the device is locked. I tested on two different devices and it worked fine. As I understand, IntentService will be killed once the execution is done. I want a process to be running in the background for ever to listen for user activities, whenever the user shakes the mobile, I need to alert the users based on criteria. I believe, the device turns into sleep mode as soon as I lock the device and screen is off. – Ramesh Sangili Jan 02 '13 at 21:15
  • 1
    @Ramesh: "I want a process to be running in the background for ever to listen for user activities, whenever the user shakes the mobile, I need to alert the users based on criteria" -- that will require you to keep a `WakeLock` acquired constantly, with corresponding harm to the user's battery life. Your users will reward you with low ratings on the Play Store. – CommonsWare Jan 02 '13 at 21:19
  • 1
    Sorry to bug you, how gmail,facebook,linkedin syncs the mails or updates automatically every x mins? do they also use WakeLock? or do we have something else which is efficient to run a process forever? – Ramesh Sangili Jan 02 '13 at 21:33
  • @Ramesh: "how gmail,facebook,linkedin syncs the mails or updates automatically every x mins?" -- some may use a service with `AlarmManager`, some may implement a `SyncAdapter`. "do they also use WakeLock?" -- certainly the service ones would. I have not played with `SyncAdapter` yet, but my hope is that Android manages the `WakeLock` for you in that scenario. – CommonsWare Jan 02 '13 at 21:36
11

I used the code below in an app.

Make sure your service is sticky:

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    //this service will run until we stop it

    return START_STICKY;
}

I you want your phone to be awake constantly u can use this code below:

private WakeLock wl;

PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "whatever");
    wl.acquire();

Don't forget the permissions in your manifest.

<uses-permission android:name="android.permission.WAKE_LOCK" />
Jasper Fioole
  • 449
  • 1
  • 5
  • 25