Bind service to activity in Android
"If you start an android Service with startService(..)
that Service will remain running until you explicitly invoke stopService(..)
.
There are two reasons that a service can be run by the system. If someone calls Context.startService()
then the system will retrieve the service (creating it and calling its onCreate()
method if needed) and then call its onStartCommand(Intent, int, int)
method with the arguments supplied by the client. The service will at this point continue running until Context.stopService()
or stopSelf()
is called. Note that multiple calls to Context.startService()
do not nest (though they do result in multiple corresponding calls to onStartCommand()
), so no matter how many times it is started a service will be stopped once Context.stopService()
or stopSelf()
is called; however, services can use their stopSelf(int)
method to ensure the service is not stopped until started intents have been processed.
Clients can also use Context.bindService()
to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate()
while doing so), but does not call onStartCommand()
. The client will receive the IBinder
object that the service returns from its onBind(Intent)
method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the Service's IBinder
). Usually the IBinder
returned is for a complex interface that has been written in AIDL.
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()
."
Bind service to activity or fragment?
Bind the Service
to your activity and not the Fragment
. The description of your application, one activity with multiple Fragment
that are swapped in and out, makes this the most (and really only) practical approach.
When you bind a Service
to an Activity
you are tying its lifecycle to that of the Activity
. See Bound Services. Each time you add or remove a Fragment
in your activity that Fragment
is created and destroyed. You do not want to try to link a service to this process because then you would have to create and destroy the service each time a new fragment is created or destroyed.
Instead bind to the host Activity
. You can then interact with your host activity from your fragments with an interface to access the bound service or by Intent
.
How can I bind my Activity to my currently running Service in Android?
You might want to have a look at this: https://github.com/JonasGroeger/GPSService
bind service with activity
Bound services can get really complex take your pick of the methods.
http://developer.android.com/guide/components/bound-services.html
Extending ibinder only works for services in the same process. So if you goal is to build a super memory efficient service that runs in its own process you can't use this.
Using a messenger http://developer.android.com/guide/components/bound-services.html#Messenger Eclipse ADT LINT will show warning that the handler should be static. I made it static by using a weakReference to the context which I setup in onCreate for making application calls.
private static WeakReference<Context> weakContext = null;
public void onCreate() {
ServiceLocationRecorder.weakContext = new WeakReference<Context>(
getApplicationContext());
}
Get the context with the following code.
Context context = weakContext.get();
if (context != null) {
AIDL http://developer.android.com/guide/components/aidl.html
I tried AIDL and builds take a lot longer so If you have a slow machine like mine you'll be disappointed with the build times but it does work.
Running Bindservice after the activity is destroyed
You can startService then bindService from activity. That way when activity can unbind when it's done but music service will keep playing until stop self is called. So use both mechanisms are required to stop.
Related Topics
Android Hide/Unhide App Icon Programmatically
Google Signin Not Working in Release Mode APK Android
How to Get All Android Contacts But Without Those Which Are on Sim
Java.Lang.Noclassdeffounderror: Javax.Activation.Datahandler in Android
Use Tab with New Toolbar (Appcompat V7-21)
Installation Failed Due To: 'Null' - Android Studio 3.5
How to Securely Store Credentials (Password) in Android Application
How to Write Vertically in a Textview in Android
How to Simulate :Active CSS Pseudo Class in Android on Non-Link Elements
Android Canvas: Draw Transparent Circle on Image
How to Make Constraintlayout Work with Percentage Values
Call Method When Home Button Pressed
How to Avoid "Illegalstateexception: Scrollview Can Host Only One Direct Child"
Android: How to Overlay a Bitmap and Draw Over a Bitmap
Changing Background Color of Selected Item in Recyclerview
How to Call a Local Web Service from an Android Mobile Application