Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long running Android 'service'

I have an Android app, in which Activities fire long running operations that run in the background. These operations interact with the Activities when done. I'm developing a component that handles the Activity/Long-Running-Task coupling, taking care of activities being destroyed and recreated.

Right now that component is implemented as an Android service. The activities call bindService and use the resulting IBinder to start and track tasks. I decided against using startService, because I prefer the richer API possible through a Java interface.

Now the problem. Activity A start ups, binds to the service and calls serviceApi.runTask(...). Activity A is then destroyed (because the user flips the phone, for instance) and recreated as Activity A'. A' then binds again to the service, announces its existence and everything should be running nicely.

Except that my Service gets destroyed. When Activity A is destroyed, it unbinds from the service. Android sees there are no more clients, and kills the service. When Activity A' is created, the service is created again, and I lose everything the old service had.

The only solution I can see is using a singleton for the service. And then it doesn't really have to be an Android service, just an instance that's accessible to everyone. Is that frowned upon in Android? Is there a better design that fits this problem?


Editted: Even if I call startService and then bind to it, nothing guarantees that the service instance will exist as long as the application is running. Android can kill sticky services if resources are low. Killing the service will cause the application to malfunction, and I can't have that.

like image 644
zmbq Avatar asked Nov 13 '11 18:11

zmbq


People also ask

How do I run service forever on Android?

Call startService() method in Foreground Service Launcher class in your Activity to start a background service which will run forever. In order to sync something between application and server we can create a handler which run every time interval .

What is the life cycle of services in Android?

Lifecycle of Android Services. Android services life-cycle can have two forms of services and they follow two paths, that are: Started Service. Bounded Service.

How do you check if a service is already running Android?

You can do this by making your own Interface where you declare for example " isServiceRunning() ". You can then bind your Activity to your Service, run the method isServiceRunning(), the Service will check for itself if it is running or not and returns a boolean to your Activity.

What happens to service when app is killed Android?

If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped. So what you can do is, send broadcast from onTaskRemoved method of your service as follows: Intent intent = new Intent("com.


2 Answers

Even if I call startService and then bind to it, nothing guarantees that the service instance will exist as long as the application is running.

Correct.

Android can kill sticky services if resources are low.

Also correct. All "sticky" means is that Android might restart the service.

Killing the service will cause the application to malfunction, and I can't have that.

It is impossible to create a service that is guaranteed to run forever. For starters, users can get rid of your service whenever they want, because users detest developers who have pointless services that run forever. Writing everlasting services is necessary only in very few cases; otherwise, it's just sloppy programming.

The only solution I can see is using a singleton for the service. And then it doesn't really have to be an Android service, just an instance that's accessible to everyone. Is that frowned upon in Android?

Singletons (a.k.a., static data members) will go away when the process is terminated. The process will be terminated eventually, particularly if there are no active services and none of your activities is in the foreground..

like image 60
CommonsWare Avatar answered Oct 24 '22 02:10

CommonsWare


Call startService and in onStartCommand return START_STICKY. It should keep the service going.

You may also want to look into foreground services:

http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)

like image 21
Kevin Galligan Avatar answered Oct 24 '22 02:10

Kevin Galligan