1

I have one scenario for my application. This is as follow: I want to use countdown timer in background service and it also run in background even if application is removed from stack. Because I want to generate notification for specific time interval even application is not in a stack. Please give me some advice how to manage service after application being destroyed. Thanks in advance.

ifeegoo
  • 7,054
  • 3
  • 33
  • 38

2 Answers2

1

A service is a component which runs in the background without direct interaction with the user. As the service has no user interface, it is not bound to the lifecycle of an activity.

A Service can have two forms:

1) Started/Unbound: In this case, an application component starts the service by calling startService() , and it would continue to run in the background, even if the original component that initiated it is destroyed. For instance, when started, a service would continue to play music in the background indefinitely.

2) Bound: An Android component may bind itself to a Service using bindservice (). A bound service would run as long as the other application components are bound to it. As soon as they unbind, the service destroys itself.

Service

A Service is not a Thread so you have to implement a Thread inside the service.

Erythrozyt
  • 802
  • 6
  • 21
  • "indefinitely" The system attempts to keep running services around as much as possible. The only time they should be stopped is if the current foreground application is using so many resources that the service needs to be killed. – csenga Dec 03 '15 at 11:33
  • If the service is currently executing code in its onCreate(), onStartCommand(), or onDestroy() methods, then the hosting process will be a foreground process to ensure this code can execute without being killed. indefinitely is a hard word but you could also use startForeground(int, Notification) It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern. – Erythrozyt Dec 03 '15 at 14:10
0

Check AlarmManager.

Alarms have these characteristics:

  • They let you fire Intents at set times and/or intervals
  • You can use them in conjunction with broadcast receivers to start services and perform other operations.

  • They operate outside of your application, so you can use them to trigger events or actions even when your app is not running, and even if the device itself is asleep.

  • They help you to minimize your app's resource requirements. You can schedule operations without relying on timers or continuously running background services.

A working example can be found here.

Community
  • 1
  • 1
csenga
  • 3,819
  • 1
  • 21
  • 31