Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding and connecting an android service outside an activity

Tags:

android

I would like to bind and connect a service but not within an android activity. Is there a class witch could be extended to have a context necessary for binding?

What i am trying to do is to provid a simple java library using an android service. My library does not use a UI. I only need to bind and connect my service inside a class witch necessary have application context necessary to the binding

Thanks

like image 920
Andro Ap Avatar asked Dec 23 '11 15:12

Andro Ap


People also ask

How do you bind a service to an activity?

To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.

What is Android bond service?

A bound service is like a server in a client-server interface. A bound server allows components, such as activities, to bind to the service, send requests, receive responses and even perform IPC or Inter-Process Communication.

What is bound and unbound service in Android?

Bounded services are bounded to an activity which binds it and will work only till bounded activity is alive. while a unbounded service will work till the completion even after activity is destroyed.

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

You can get the context from your application class. Derive your own class from Application, and give it a static getApplication method. You can use that for creating services.

Note that without an Activity, binding to a service may be a little hard - if, for example, you're in a BroadcastReceiver, it's not going to be alive long enough for you to receive the callback after the service has been bound.

like image 74
zmbq Avatar answered Nov 10 '22 04:11

zmbq