0

In my app i'm sending some data to the server in an regular interval (every 5 min)for this I used IntentService. IntentService checks for data and if data is available it sends that data to server. so here my question do i need to acquire wake lock every time whenever service sends a data to server and released it immediately after the network call??? what is the efficient way to acquire & release wake lock. My ultimate goal is to save battery.

Prachi
  • 2,559
  • 4
  • 25
  • 37

2 Answers2

0

You need to acquire a wake lock. Actually it's more complicated than that:

  1. register an alarm with the alarm manager
  2. register a receiver to receive the alarm (this holds a wakelock)
  3. in the receiver delegate to WakefulIntentService

Alternatively look at WakefulBroadcastReceiver

See: https://stackoverflow.com/a/28396826/281545

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • He know how to acquire a wake lock but he wanted to know the efficient way to do that as he said " what is the efficient way to acquire & release wake lock. My ultimate goal is to save battery." not How to. – ghost talker Mar 25 '15 at 12:29
  • @ghosttalker: he is using an intent service - wrong + he asks _so here my question do i need to acquire wake lock every time whenever service sends a data to server_ - answer yes - but acquiring a lock in the service won't do - hence my answer. – Mr_and_Mrs_D Mar 26 '15 at 19:13
-1

If you are using the background service to send the data to the server then why you need to acquire the wake lock here? I think there is no need to do this. But in case if you want to notify the user for the completion of uploading task, then you are better to play some sound using sound pool, that will notify user that the uploading has been completed.

ghost talker
  • 402
  • 5
  • 15
  • Its a background task no need to notify user...actually i read on forum(http://stackoverflow.com/questions/9309044/service-pauses-on-screen-lock?rq=1) that service stops when phone goes to asleep....thats why post this question – Prachi Mar 25 '15 at 09:36
  • No service never stops. its work on background always – ghost talker Mar 25 '15 at 09:50
  • well in this discussion It seems that service goes off as the mobile/device goes to asleep. But I have used service many times and it does not goes off even when the device is asleep. – ghost talker Mar 25 '15 at 10:20
  • I think this is because I made my custom service and I designed it such a way that it never gets off until and unless user himself closes the service. So you do not have to make the service sticky. – ghost talker Mar 25 '15 at 10:21
  • Means your own made service class. – ghost talker Mar 25 '15 at 10:28
  • but that class also extending service or intentservice right?? – Prachi Mar 25 '15 at 10:31
  • service I think intent service or making service sticky causes a service to go to pause when the application is paused or inactive. In other words you should not bind your service with the activity if you want to achieve your above stated goal. – ghost talker Mar 25 '15 at 10:33
  • i didnt bind service with activity – Prachi Mar 25 '15 at 10:39
  • He needs a wakelock - -1 – Mr_and_Mrs_D Mar 25 '15 at 12:08
  • but we are discussing something else please follow through the whole discussion – ghost talker Mar 25 '15 at 12:28
  • 'If you are using the background service to send the data to the server then why you need to acquire the wake lock here' -> he needs it - edit this part – Mr_and_Mrs_D Mar 25 '15 at 18:55