Getting data from an Unbound Service in Android - android

I currently I have an unbound service that is running continually grabbing my gps position that I start on boot. I then have an app that is suppose to plot where I've been by pulling data from the service.
I can't bind the the service to talk to it or it will be destroyed once I close the app.
Is there any good way to get data from an unbound service or keep a bound service from dying once I unbind it?
Cheers! :)

There is a workaround for keeping your service alive. Call your service by calling startService and then bind to the service. This way your activity is maintaining the the lifecycle of the service.

As the documentation states:
A service can be both started and have
connections bound to it. In such a
case, the system will keep the service
running as long as either it is
started or there are one or more
connections
So first, start the service with startService(), then bind to it in onResume(), and unbind in onPause(). The service will continue to run nevertheless because it is started. And when you want to stop the service either call stopSelf() from within the service or stopService() from an activity. It will stop right away or as soon as you unbind from it if there's any connection alive.

Related

Android services and deference [duplicate]

Can anybody explain what is difference between unbound and bound service in android and explain about intent service
Thanks
Bound Service
A service is bound when an application component binds to it by
calling bindService(). A bound service offers a client-server
interface that allows components to interact with the service, send
requests, get results, and even do so across processes with
interprocess communication (IPC).
When the last client unbinds from the service, the system destroys the
service EXCEPT If the service was started by
startService
Unbound Service or Started
A service is started when an application component, such as an
activity, starts it by calling startService(). Once started, a service
can run in the background indefinitely, even if the component that
started it is destroyed.
BUT
Most confusion about the Service class actually revolves around what it is not:
A Service is not a separate process. The Service object itself does
not imply it is running in its own process; unless otherwise
specified, it runs in the same process as the application it is part
of.
A Service is not a thread. It is not a means itself to do work off of
the main thread (to avoid Application Not Responding errors).
That is where IntentService are used.
IntentService is a subclass of Service that uses a worker thread to
handle all start asynchronous requests (expressed as Intents) on
demand, one at a time. Clients send requests through
startService(Intent) calls; the service is started as needed, handles
each Intent in turn using a worker thread, and stops itself when it
runs out of work.
hope it helps :)
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.
a tabular difference is given in below link which is very useful for interviews
http://infobloggall.com/2014/04/15/bounded-service-in-android/
Unbound service is started when component (like activity) calls startService() method
Where As A service is bound when another component (e.g. client) calls bindService() method.
The Unbound service can stop itself by calling the stopSelf() method.
Where As The Bound service cannot be stopped until all clients unbind the service.
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.
Thanks
Bound and Unbound Services are not two sides of a coin
A service can be a bound or unbound(started) or both, It is just the matter of implementation you provide to the callback methods of Service class. See all four callback methods here
But for the sake of differentiation here you go
1. Staring a service
Unbound Service is started by calling startService() method.
Bound Service is started by calling bindService() method.
However in both calls system calls onStartCommand() method internally
2. Life Span of a service
Once an unboundService is started it runs indefinitely until
Application component calls stopService() method
Service itself calls SelfStop() method.
BoundService runs as long as the service is bound to a client. When there is no active client bound with the service, the system destroys the Service
3. onBind() method
When you are writing a service you will have to override the onBind(). If
Unbound Service then return null
BoundService then return IBinder object.
Though unbound services does not return Ibinder object it does not mean that it can not interact with application component.
There are ways to do that for example BroadCastReceiver or ResultReceiver
One way vs Two-way communication with Service
When you want two-way communication with your Service then you should bind your service with Activity.
Eg. Playing music in the background with pause, play option (Activtiy <-> Service).
Go with unbound or started service when you just want your Service to update your Activity (Service->Activity).
Eg: Timer Service which updates Activity every second.
Another example
You have written some Service which deals with Location changes.
If you want to update your activity when you move 10 meters (Go with unbound service).
If you want to see the coordinates of your current location when you click some button in the activity. (Go with the bound service).

Can anybody explain what is difference between unbound and bound service in android

Can anybody explain what is difference between unbound and bound service in android and explain about intent service
Thanks
Bound Service
A service is bound when an application component binds to it by
calling bindService(). A bound service offers a client-server
interface that allows components to interact with the service, send
requests, get results, and even do so across processes with
interprocess communication (IPC).
When the last client unbinds from the service, the system destroys the
service EXCEPT If the service was started by
startService
Unbound Service or Started
A service is started when an application component, such as an
activity, starts it by calling startService(). Once started, a service
can run in the background indefinitely, even if the component that
started it is destroyed.
BUT
Most confusion about the Service class actually revolves around what it is not:
A Service is not a separate process. The Service object itself does
not imply it is running in its own process; unless otherwise
specified, it runs in the same process as the application it is part
of.
A Service is not a thread. It is not a means itself to do work off of
the main thread (to avoid Application Not Responding errors).
That is where IntentService are used.
IntentService is a subclass of Service that uses a worker thread to
handle all start asynchronous requests (expressed as Intents) on
demand, one at a time. Clients send requests through
startService(Intent) calls; the service is started as needed, handles
each Intent in turn using a worker thread, and stops itself when it
runs out of work.
hope it helps :)
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.
a tabular difference is given in below link which is very useful for interviews
http://infobloggall.com/2014/04/15/bounded-service-in-android/
Unbound service is started when component (like activity) calls startService() method
Where As A service is bound when another component (e.g. client) calls bindService() method.
The Unbound service can stop itself by calling the stopSelf() method.
Where As The Bound service cannot be stopped until all clients unbind the service.
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.
Thanks
Bound and Unbound Services are not two sides of a coin
A service can be a bound or unbound(started) or both, It is just the matter of implementation you provide to the callback methods of Service class. See all four callback methods here
But for the sake of differentiation here you go
1. Staring a service
Unbound Service is started by calling startService() method.
Bound Service is started by calling bindService() method.
However in both calls system calls onStartCommand() method internally
2. Life Span of a service
Once an unboundService is started it runs indefinitely until
Application component calls stopService() method
Service itself calls SelfStop() method.
BoundService runs as long as the service is bound to a client. When there is no active client bound with the service, the system destroys the Service
3. onBind() method
When you are writing a service you will have to override the onBind(). If
Unbound Service then return null
BoundService then return IBinder object.
Though unbound services does not return Ibinder object it does not mean that it can not interact with application component.
There are ways to do that for example BroadCastReceiver or ResultReceiver
One way vs Two-way communication with Service
When you want two-way communication with your Service then you should bind your service with Activity.
Eg. Playing music in the background with pause, play option (Activtiy <-> Service).
Go with unbound or started service when you just want your Service to update your Activity (Service->Activity).
Eg: Timer Service which updates Activity every second.
Another example
You have written some Service which deals with Location changes.
If you want to update your activity when you move 10 meters (Go with unbound service).
If you want to see the coordinates of your current location when you click some button in the activity. (Go with the bound service).

Bind unbound Services in Android

I'm developing a Android Service. I would like the service to run even when the application not is active. So I start it without binding it:
startService(new Intent(Service.class.getName()));
Now it will run continuously until I choose to stop it, right?
If I, from another activity, bind the service will it stop when I unbind it?
Not necessarily. However, you have to remember that if there is memory pressure, it may be killed depending on the priorities (and if it is unbound, any visible app will probably have higher priority). The lifecycle is described here:
http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
When the last client unbinds from the service, the system destroys the service (unless the service was also started by startService()).
It mean that your service will not stop even you unbind service from all activity in case of you had started service as startService()
For more details about bound service follow bellow link
Bound services

How to create an android service that runs after the activity has ended

I have a android app that has an ongoing service for tracking a persons location. I want to be able to start up the service from within the activity and end it from within the activity. When the activity is running I need to be able to bind to the service and communicate to it via aidl. However I need the service to continue even when the activity has ended. I only want the service to end when the it is told to by the activity.
So far I have my activity and service and they can communicate through a aidl file. But at the moment the service ends when the activity does.
How do I get it to continue running when the activity has ended AND how do I rebind to it when the activity is restarted?
I have figured it out.
I was using the bindService method which created my service for me and bound to it (I need a binding), but if a service is created via a call to bindService the service only lives as long as the binding and so when calling unbindService, which I was doing when my activity was destroyed, the service itself also was destroyed.
However if you create your service with a call to createService and then call bindService you will bind to the already running service. Then when you come to unbind from it, it no longer destroys itself. The service will continue until you call stopService.
Then when you re-enter your activity you can check if the service is already running and if it is just call bindService other wise just call startService followed by bindService

Binding to a service in Android

I'm a little confused on how binding to services works. I understand using Context.startService() starts the service and that bindService doesn't call onStartCommand. But my understanding is that if I use startService, I have to explicitly stop the service. But I want the service to die if there are no more activities bound to it.
My problem is that calling bindService never calls onServiceConnected(), so my Service binder object is null. Does the service have to be explicitly started in order to bind to it? If so, how does it know to terminate when nothing is binding to it anymore, and how do I know if it's started so I can know to use the bound object?
if you call bindService with BIND_AUTO_CREATE as flag the system will bind your activity to the service if it exists, otherwise if it doesn't exist the system will start the service for you and then it will bind your activity to the service. Furthermore if you start a Service in this way the service will remain active only if it has still some context binded.
this is from bindService():
Connect to an application service, creating it if needed. This defines a dependency between your application and the service. The given conn will receive the service object when its created and be told if it dies and restarts. The service will be considered required by the system only for as long as the calling context exists. For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed
and this is from ServiceLyfecycle
A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated. All cleanup (stopping threads, unregistering receivers) should be complete upon returning from onDestroy().
The answer is that I wasn't waiting for the service to actually be bound before using it, since it gets bound asynchronously
You should be setting up your binder in onBind and any generic setup in onCreate. The behaviour of starting and binding services is explained at http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle

Categories

Resources