bbbwex
bbbwex

Reputation: 732

Android service && app

I'm just reading up on the use of services to keep an app alive in the background.

A few things are not quite clear to me.

1: Once a service has started, does it stay alive when the main app gets destroyed by Android? (I know it does with OnPause() and OnStop() )

2: If anything is declared in memory for the service, is there a way to access this as well from my app?? (EG service just records the GPS to see if you're moving or standing still. from the main app I want to see how much of each is recorded while the main app was inactive)

I know these are fairly general questions, I'm just reading up on this part of Android programming, and would like to modify a program in the near future. So I have no code to go with the question yet

Thanks,

BBBwex

Upvotes: 0

Views: 56

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006604

Once a service has started, does it stay alive when the main app gets destroyed by Android?

An app does not get destroyed. Activities get destroyed. Apps have their process terminated.

A service will run in the process until:

  • there are no more bound connections (i.e., via bindService()) and

  • if the service was started with startService(), it was stopped with stopService() or stopSelf()

Of course, once the process is terminated, the service (and everything else) is gone.

If anything is declared in memory for the service, is there a way to access this as well from my app?

Your service is part of your app. I am going to assume here that by "app" you mean "activity".

Your service has any number of ways of publishing information in ways that an activity can monitor and use, including:

  • Service writes the data to a ContentProvider, which updates the activity via a Loader or ContentObserver

  • Service sends messages to the activity, via LocalBroadcastManager, a third-party message bus like Otto, a Messenger tied to a Handler, etc.

  • Service stores a cache of data in a static data member, which the activity reads (or perhaps polls)

  • The activity simply reads the data out of whatever persistent data store the service uses (e.g., SharedPreferences) as needed

  • Etc.

Upvotes: 1

Related Questions