0

I want to periodically fetch data from server and put in database without opening the app so I want my service always open with or without activity knows about it.
So I want to keep my service always open. This means I want my service not bounded by activity but right now when I long press and exit from app, the service stops.

Can somebody knows how to solve it. Thanks Many apps like WhatsApp Facebook do it. Even though we long press and swapped out the app, the service and process still not closed. I want similar functionality

enter image description here

user3265443
  • 535
  • 1
  • 8
  • 25

2 Answers2

0

You can use foreground process if you need it always alive. Or a better solution is to listen for system broadcasts and start your service based on those.

For example it will be a great idea to start your service synching when the user starts charging his device.

Also you might be interested reading about Manipulating Broadcast Receivers On Demand to manage the battery life even better.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
0

When you create a Service, in its onStartCommand() method, return START_STICKY. That way, even when you remove your application from recent Apps list, the service is killed first and then restarts again. This is because removing an application from recent apps list destroys that process. As Service is part of the same process, it will also get killed. As we are returning START_STICKY, service will start itself again. You can read about this at link

user2430771
  • 1,326
  • 4
  • 17
  • 33