Hey I need to implement a infinite loop for receiving commands through socket and updating status in my android app and want that thread to be running even if application is closed. So I need to know that what I'm thinking is optimal solution or not... I am planning to make a service which create a thread which will look for commands and this class is also Observable so the activity can get updates of status from it. Kindly suggest your way or if you think this is right solution. Thanks.
Asked
Active
Viewed 696 times
1 Answers
1
Running things in background is usually a bad idea(battery life), especially if using data services(data plan costs). What you need is indeed a service, but start your updating status thread only when you receive a user present broadcast and stop it when the device goes to sleep(I think it's enough just not to request a wake lock and not use startForeground()).

crios
- 609
- 6
- 6
-
hey thanks for the help. I am actually making home automation project in which andriod device is the center unit which control appliances. So commands can be delivered through internet or wireless local network using sockets... so there has be continuous thread running which will parse command and send to hardware.(will update status on application only when the activity is running) And it will get the reply from hardware and send back to the command sender. – Adnan Ahmed Mar 23 '13 at 23:17
-
Nice, thought it was a mobile device. Good luck :) – crios Mar 24 '13 at 07:21