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.
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.
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 aWifiLock
, 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 aWakeLock
acquired only for a brief period of time, to complete some specific task. In your case, you need to keep yourWakeLock
acquired indefinitely, as you have no idea when work might need to be done.In the service, presumably. You will probably acquire the
WakeLock
inonCreate()
of the service and release it inonDestroy()
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 thisWakeLock
is in force.