Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a Service to an android.app.Activity vs Binding it to an android.app.Application

Is there any fundamental difference in binding a service to an android.app.Activity vs binding it to an android.app.Application. I want to bind the service to an Application because I want to keep some global state/data in the Application instead of duplicating it in all my activities.

Thanks.

like image 520
Soumya Simanta Avatar asked Jul 01 '10 03:07

Soumya Simanta


People also ask

What is the difference between activity and service in Android?

Services are a unique component in Android that allows an application to run in the background to execute long-running operation activities, on the other hand, an activity, like a window or a frame in Java, represents a single screen with a user interface.

What is service binding in Android?

A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC).

What is the difference between bound and unbound service in Android?

The Unbound service runs in the background indefinitely. Where As The Bound service does not run in the background indefinitely. The Unbound service is stopped by stopService() method. Where As In The Bound service, The client can unbind the service by calling the unbindService() method.

What is difference between started and bound services in Android?

Started services run until they are stopped or destroyed and do not inherently provide a mechanism for interaction or data exchange with other components. Bound services, on the other hand, provide a communication interface to other client components and generally run until the last client unbinds from the service.


1 Answers

No. There is no fundamental difference.

That said, subclassing android.app.Application is a very good place to store global/state data. There is only one instance and everything that derives from Context has access to it.

I'm also sure that binding a service to an application will result in some odd lifetimes if you aren't careful. What I mean is that even though your app is out of sight and has no activities alive, your application could still exist because your service still exists. Your service still exists because your application still exists. You would have to manually shut down the service based on some event other than onDestroy.

like image 142
Jere.Jones Avatar answered Oct 05 '22 23:10

Jere.Jones