Long lasting Android service

I have an Android app in which Activities runs lengthy operations that run in the background. These operations interact with actions upon completion. I am developing a component that handles the Activity / Long-Running-Task interaction, taking care that the actions are destroyed and recreated.

Now this component is implemented as an Android service. Actions are called by bindService and use the received IBinder to start and track tasks. I decided not to use startService, because I prefer to use a richer API through the Java interface.

Now the problem. Exercise Getting started, binding to a service, and calling serviceApi.runTask (...). Then, action A is destroyed (because the user, for example, flips the phone) and recreated as activity A '. A 'then contacts the service again, announces its existence, and everything should work well.

Except that my Service is destroyed. When action A is destroyed, it drops out of service. Android sees that there are no more clients, and it kills the service. When action "A" is created, the service is created again, and I lose everything that the old service had.

The only solution I see is to use Singleton for the service. And then it really should not be an Android service, but just an instance accessible to everyone. Did it frown on Android? Is there a better design that is suitable for this problem?


: startService , , , . Android , . , .

+5
3

startService , , , .

.

Android , .

. "" - , Android .

, .

, . -, , , , , . ; .

, , - Singleton . Android, , . Android?

(a.k.a., ) , . , , .

+7

startService onStartCommand START_STICKY. .

:

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

+1

I need to create ongoing service. See this manual .

In a few words - do not call bindService, call startService.

0
source

All Articles