1

I have a service that executes in 2 phones. They communicate with each other using sockets and execute some script (which may take 1-2 minutes approx.) and exchange files (using HTTP Post) between them.

I want this execution to continue, even when the screen is off, i.e acquire a Wakelock. I saw this link, but I'm not sure where to incorporate this in my code(as the device may sleep in the mid of execution). Any guidance is highly appreciated. Thank you.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
user1741274
  • 759
  • 3
  • 13
  • 25
  • "They communicate with each other using sockets and execute some script (which may take 1-2 minutes approx.) and exchange files (using HTTP Post) between them." -- and what is the trigger mechanism for this work? – CommonsWare Nov 13 '12 at 11:29
  • I m using same app for 2 phones. In onCreate() it starts a service listening on a port. Clicking a button in phone1, does a HTTP Post of a file to phone2 and the communication keeps going on(as phone 2 also listens on a port initially). @CommonsWare – user1741274 Nov 13 '12 at 11:34

2 Answers2

2

I m using same app for 2 phones. In onCreate() it starts a service listening on a port. Clicking a button in phone1, does a HTTP Post of a file to phone2 and the communication keeps going on(as phone 2 also listens on a port initially).

Well, your primary objective seems to be to cause the owner of phone2 to attack you with a scimitar, since the phone2 will run out of battery.

That being said, your HTTP daemon will need to be in a service, where you acquire and release a WakeLock (and presumably a WifiLock, since what you want won't work over most mobile data connections).

WakefulIntentService is not useful here, because it is designed for sensible scenarios, where we need to keep a WakeLock acquired only for a brief period of time, to complete some specific task. In your case, you need to keep your WakeLock acquired indefinitely, as you have no idea when work might need to be done.

I m not sure where to incorporate this in my code.

In the service, presumably. You will probably acquire the WakeLock in onCreate() of the service and release it in onDestroy() of the service. If you wish to avoid a scimitar-related demise, you will make sure that the user has plenty of control over exactly when this service is running, and therefore have plenty of control over when this WakeLock is in force.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have added the wake lock and wi-fi locks. My service starts executing after some time(say 5 minutes) after the screen is locked(When I view the logs in Logcat). I ve acquired a partial wakelock. Is this an expected behavior? @CommonsWare – user1741274 Nov 13 '12 at 14:56
  • @user1741274: "Is this an expected behavior?" -- I have no idea, as I did not write your code. – CommonsWare Nov 13 '12 at 16:50
  • Sorry. I have acquired a partial wake lock. I meant to ask: there is a time variation between execution of task each time after I lock my screen i.e, sometimes task executes after 5 minutes or sometimes after 10 minutes(after I lock the screen). Any idea as to why it varies? @CommonsWare – user1741274 Nov 14 '12 at 15:24
  • @user1741274: "Any idea as to why it varies?" -- I have no idea, as I did not write your code. I do not know what is triggering the "task" to "execute", and hence I have no idea why anything might affect that. – CommonsWare Nov 14 '12 at 16:50
  • I have implemented WakeLock(using PowerManager), acquired when the service starts and released when the service ends. I want to monitor battery consumption. While testing, I notice that when the device is plugged in, it seem to work fine. When it is disconnected, the service seems to stop. Is this expected behavior? Please guide me. @CommonsWare – user1741274 Dec 05 '12 at 07:06
1

Why not implement that functionality with a service? See here. I'm not very experienced in android development but have worked recently in a project where a functionality similar to that was implemented using services.

rmcc
  • 743
  • 1
  • 6
  • 14
  • Yes I m implementing as a service(which starts on click of a button in UI). Please see line 1 @rmcc – user1741274 Nov 13 '12 at 10:38
  • So, I'm not understanding correctly what is the problem. If you have a service, why shoud it not continue working when the screen is off? – rmcc Nov 13 '12 at 10:44
  • In phone 1, my service starts, calls an intent(that executes some script in a file) and notifies after completion using Broadcast. In onReceive() method defined in the service, I am posting the updated file to II phone and it goes on. @rmcc – user1741274 Nov 13 '12 at 10:52
  • In here: http://developer.android.com/guide/components/services.html a very good documentation for services, as you probably know. I'm not fully understanding what is the real problem. As you can see in the beginning of the document it has a description of what a started service is that seems what you are trying to accomplish. Also take a look at Running a Service in the Foreground. – rmcc Nov 13 '12 at 10:57
  • When I run my app, when the screen goes off, the application stops. So I need a Wake-Lock for this. I m not sure where to incorporate this in my code. @rmcc – user1741274 Nov 13 '12 at 11:52
  • If you are running a service, that shouldn't happen. Maybe there's something wrong with your service's implementation. I happen to be working on an application that if a determined event happens the service has to warn the user. It's fully implemented with a service and works fine. – rmcc Nov 13 '12 at 15:29
  • Hello again. I wasn't quite sure about what the problem was until I reproduced it. I got around the same problem when I implemented a service that was reading values from the accelerometer. When the screen is locked/goes off the system goes into power saving mode and disables some services. Sensor service is one of them and as I checked around your problem also happens. The way I worked around this was by acquiring a wakelock on onCreate and disabling when not not needed. In your case you can acquire it the transfer starts and release it when it's over. – rmcc Nov 15 '12 at 10:31
  • You can use the link that is given in the question for implementation. Ya in my case, I have implemented already :) – user1741274 Nov 15 '12 at 12:36
  • I solved it on my end. The only problem is the battery consumption. It goes through the roof if you try to get a continuous reading of accelerometer values. – rmcc Nov 15 '12 at 15:04