When to bind a service - android

I'm developing an application which have a main activity with 5 tabs. In other words, that main activity contains a framelayout that can be replaced with 5 different fragments. Furthermore, the application can navigate to others activities from that main activity, but the user will have to return to that main activity to keep navigating through the application. On the other hand, I have a started service that is started on the application's onCreate method, this service gets some data from our API, and some of that data is used for the next fragments/activities: - 2 out of 5 fragments that may be replaced in the main activity. - Some of the others activities that may be started from that main activity.
So I was wondering about the best way to bind to that service (to get that data). I've thought 3 options:
1) Each fragment/activity that needs the data of the service binds to it on its own onStart method, and unbinds the service on its onStop. So each fragment/activity would be totally independent from the other fragments/activities.
2) The activities that needs the data from the services and the main activity (instead of the fragments) binds to the service. So the service will not be bound/unbound when navigating through the tabs, but will be bound even if the user is in a fragment that is not required.
3) Create a singleton on the application's onCreate method that binds to the service, so the fragments/activities that needs some data from the service would use that singleton instead of binding/unbindig to the service.
I have currently developed the first option, but I keep thinking about which option is the best. What do you think? May you lend me a hand?
Thanks in advance!

I use an application scoped singleton in these cases. Fragments/Activities can just access the service whenever they need to without having to worry about stopping and starting anything. I have an example here.

Related

A Local Service implementation closely related to Application Life Cycle

In my App. I have extended Application class, say MyApp and I have few activities say A,B,C,D where A is the default Activity or root Activity. I want to start a Service (say MyService) inside OnCreate of the MyApp and stop it when User logs out from my Application,
However I need a strong way to communicate between my Activities (A,B,C,D), MyApp and MyService. Now Application is launched second time from the Memory. OnCreate of MyApp will not be called so I would like to start the Service from OnClick of the SignIn button which is inside activity A. Now I have a number of questions :-
I have tried both startService and bindService. Problem with startService is I don't get back the Service reference for future use. If I use bindService will that make my Service visible through out the Application life cycle till I explicitly stop it. Which way is applicable in my case ?
What is the best way to implement communication between Service and MyApp / Activities and vice a verse ? By Communication I mean two type of communication, 1. that needs UI thread i.e. start some activity, show some dialog etc. 2. Something like myService.doSomehing().
The Service has a thread which does network communication which should not be closed during the life time of the Application. START_STICKY will work for me or I need to make extra arrangements for that.
This app is supposed to run for GingerBread+ devices...
Bind the service inside your extended app oncreate and create a public method
(getBinding) which returns that binding object ... In your root/default activity 'A' oncreate via getBinding method on casted getApplication object retrieve .. unbind when all activities close (keep counter with each getBinding method)
check this Cleanly binding/unbinding to a Service in an Application
so for 1) Bind service
2) the binding obj
3) START STICKY would be enough

How to access to the service bound by the parent activity

I have an activity that binds to a service. The service provides the functions to interact with an XMPP server.
Then, the activity launches a second activity that needs to access to the same service (for instance to initiate a voice call).
I was thinking to bind the second activity again to the same service, but it seems like an overkill, since when the second activity starts the service should already exist and should be there until the first activity destroys it on purpose (binding again means creating a new connection and waiting for the bind to happen asynchronously before using the service).
At the moment I'm using startActivityForResult() to launch the second activity and then I wait for the result and I access the service from the first activity, but I want to change the logic (the second activity must interact with the service and then the first one takes care of closing the second activity when a signal is received back from the service).
Any suggestion on how can I pass the service object to the second activity?
binding 2 activities to the same service is no overkill. It is actually the proper way of doing what i understand you are trying to do : access xmpp functions provided by a service from 2 different activities.
by binding the second activity, you will not start the service again, as it has already been started. it will connect to the same service as the first activity.
you could, of course, put all the binding to a single place, like a singleton or the Application class.
You could store a reference to it in the Application and then access it from your second activity.

android continuos polling ... how?

I have an application that needs updating constantly. I would like to create a job (Service, thread ...?) that execute the polling over all the activities. I need to update some data even if i'm not on the activity that needs to.
I would not create a service because actually I don't need to update tha application along it is closed.
What do you suggest?
Thanks
a.
I think you are confusing an Activity with the data that it presents to the user. You do not mess with other activities that are not currently on screen.
If I understand correctly, you want to update some data that is shared among your activities whenever any activity is currently visible.
You could create a Service which in turn uses a separate thread to do its polling. Then in each of your activities that you want the polling to be run you call startService in the onResume method of the activity and stopService in the onPause method of the activity.
Also make sure you use returnSTART_STICKY at the end of your service's onStartCommand method.

How to get all running activities android?

I was wondering is there a way of getting a list with all running activities ?
As far as I understand there will only one activity running at a time that will be your active activity on screen, other activities will be in paused or Stopped state if any.
No, as far as I know there is no built in way to get a list of running activities (assuming running - means non-destroyed activities).
If you only need a list of activities in your application, you can create some kind of "registrar" mechanism, so each activity registers in onCreate() and unregisters in onDestroy(). This should be some singleton class, because it should automatically be "cleared" if application is killed (as opposed to storing list of active activities in DB).

How can I bind two Android Activities to one Service?

I would like to ask for some example, where two different activities (a button in the first activity opens a second activity), are communicating with one service (AIDL, etc.).
I've tried many different tutorials, but they are only about how to make one activity → one service.
This is probably old, but I'll try to answer it anyway...
In Android, seeing as only one Activity can bind to a Service at a time, and only one Activity can be shown at a time, there isn't any real reason to want to bind two Activities at a time.
But, if you'd like, the best solution is to bind the Service in the onResume() method, and unbind it in the onPause() method. This allows you to give two unrelated Activities access to the service, while only having one bound at a time.
Each Activity is responsible for binding and unbinding from the Service. This is normally done in onResume / onPause, or onStart / onStop, depending on your requirements. One Activity cannot bind another Activity to a Service. That's just the way it is. :)
You can do it by using Messenger that provide IPC communication without using AIDL. This is how you can bind multiple activities to a service.
If you need your service to communicate with remote processes, then
you can use a Messenger to provide the interface for your service.
This technique allows you to perform inter-process communication (IPC)
without the need to use AIDL.
Have a look at this link. When you see the code, you will find a switch case within a Handler. This will cater to the multiple requests that you will send from you multiple activities/components.

Categories

Resources