0

I am building one of those SOS apps. Whenever the device is shaken above a threshold value (detected through accelerometer), I am showing a Toast (as of now)

1) App is launched. User gives name, email, etc.. and clicks finish on last screen.

2) Service is started which keeps listening for shake.

3) It detects the shake correctly if the App is running.

4) If I close the app (the activity), the service gets killed along with it.

How do I keep the service running even if app is closed, so that it can listen to shakes from background? (That's the whole purpose of this app)

[1.I am returning START_STICKY in onStartCommand

  1. I also tried using a BroadcasterReciever which will restart service by receiving broadcast from onTaskRemoved

  2. I am testing on ASUS Xenfone Max, Marshmallow OS ]

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40

3 Answers3

3

You have two options:

  • Start your service as foreground service (with startForeground(int id, Notification notification): docs. But in this case you will have to show Notification in notification tray for as long as your service is running
  • Use separate process for your service adding in manifest to your process android:process=":nameofyourprocess"
krossovochkin
  • 12,030
  • 7
  • 31
  • 54
  • Are you sure that having your Service in a separate process will have the same effect as putting it in foreground in terms of lifecycle? – JonasVautherin Feb 20 '17 at 07:20
  • @JonesV if by "lifecycle" you mean "whether these two services have the same priority for Android, so you can't tell which one will be destroyed first if Android has no more memory" - then answer is "of course no". Foreground service has highest priority and will be killed only when any other non-foreground service will be killed. But the question was about not killing Service on Activity destroy. In these terms - yes, they will behave the same. Also foreground service is like a battery killer - so if you can avoid id - do it – krossovochkin Feb 20 '17 at 07:33
  • 1
    I was just thinking that for an SOS app, you want to be sure it is running and won't get killed =). I'm interested about the "battery killer" part! Why is that? You mean even if the foreground service doesn't do anything? – JonasVautherin Feb 20 '17 at 08:02
0

You can put a Service in foreground, in which case it will always be considered as active (and it will therefore have its own notification, so the user knows that an active Service is running). It won't be stopped until it goes back to background. That is what you want in your case, as you want your Service to stay alive as long as possible. As described in the Android Service documentation:

A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory.

The idea is the same for Activities and Services, actually: when Android needs memory, it starts killing processes. The foreground processes (e.g. the Activity that is displayed on the screen, or foreground services) have a higher priority than the ones that are in background (say, a paused Activity), so they will be the last ones to be stopped by the system.

Using START_STICKY just tells the system that if it has to kill your Service, then you'd like it to restart it can. That doesn't say this Service is higher priority than the others.

JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
0

Try starting a service without binding it to the activity (Simple unbound service). Return null on your onBind() function. Sticky services attach itself to a activity and has a lifetime as long as the attachment survives. You might have a constant notification related to your application when you use foreground services.

not_again_stackoverflow
  • 1,285
  • 1
  • 13
  • 27