How to call function in main activity from service? - android

I start a service from my main activity using startService(). The service checks for sockets on localhost. When it receives a socket, it reads data from it. I would like to hide the notification bar when it receives socket with specific data. I already coded a function hideNotificationBar() which is located in my main activity. The problem is I don't know how to call this function in main activity from service.
Thanks for any help!

Send a Broadcast from Service. Register your Activity to receive this broadcast and act upon it.
This approach solves the problem of storing/acquiring the reference to Activity, which might become inactive during the lifetime of your Service.
The downside is that you can only send simple types, Bundle or classes implementing Parcelable via a broadcast Intent.

I suggest you have a look at MessengerService and MessengerServiceActivities from API Demos application.
Basically, idea is to have Handler class inside both your activity and service that handles communication between the two. Use ServiceConnection in the activity to establish connection with the service.

Related

Send data from service to bound activity periodically

I have an Activity that starts and binds to a service. It sends an intent with a List of data and the service is responsible for periodically updating that data. This is done in the handleIntent (Intent) method. What I want to do is send the updated data back to the activity. Both the activity and the service are in the same application. How can I "listen" for requests from my service? Do I have to use a Messenger and/or Broadcast Receiver? what's the cleanest, easiest, most efficient way of doing this? Thanks.
I suggest you check EventBus. it will allow you to send events like local broadcast receiver. And from that in activity you can listen specific event and display result. you can even use interface to get callback from service also.
Without any library you can achieve this with LocalBroadcastManager.
http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html
For this problem, you can use a ResultReceiver. If both the service and activity share the same ResultReceiver object then you can "push" data to the Activity from the Service using the "send(int, Bundle)" method and on the Activity side receive it with "onReceiveResult(int, Bundle)".
ResultReceiver | Android reference

Android Service Callback method to Activity

I have a Activity and a Service. I am using bindService to get the Binder object which gives my activity access to the service. So I am able to execute commands on the service easily. What I need is a way to have the service communicate back to the activity. Is the best way to handle this to make calls directly from my activity to the service via the service instance returned from the binder. Then use a local broadcast receiver to send messages back from the service to the activity?
Thanks,
Nathan
There are several ways to achieve this.
1.You can pass a handler object to the service, and have it implement the handler callback
2. You could send a broadcast
3. You could pass a listener object.
variant number 1 is usually best if you know exactly the activity you pass the reference to.
If you want several activities to handle it, better send a broadcast

How to implement the communication between a Service and an Activity in different processes?

I read some similar questions (for example at this link), but the problem I'm asking is a bit different. In fact, in my case the service is started manually by the startService method, then as a consequence it can not be started using the bindService method.
Suppose we have a package that contains the MainService service and MainServiceActivity activity. In the file "AndroidManifest.xml" this activity is declared with action MAIN and category LAUNCHER. This activity is used to configure the service via the SharedPreferences and start the service by invoking startService method. In other words, typically the user launches the MainServiceActivity and configures/starts the MainService.
Now consider another activity (Let's call it SecondActivity) that is part of another package. Depending on the configuration, the service starts this activity using the startActivity method, so this other activity is running on a separate process than the MainService. As soon as the activity is running, it should inform the service.
At this point, a communication request/reply begins between the MainService and the SecondActivity: the service sends a request and the activity sends a reply.
The communication via messaging might fit, but the MainService is started through startService method, so the bindService method can not be invoked by activities that want to bind to the service.
Then I had an idea that makes use of an additional service (Let's call it UtilityService), which is part of the same package of MainService: the UtilityService could be started using the bindService method. As a consequence:
as soon as the MainService is running, it might perform the bind to the UtilityService;
when the MainService launches an external activity (for example the above SecondActivity), this activity bind to the UtilityService.
In this way, both the MainService and the SecondActivity are connected to the UtilityService, where the latter acts as an intermediary for communication.
Are there alternatives to this idea?
In fact, in my case the service is started manually by the startService method, then as a consequence it can not be started using the bindService method.
You can both bind and start a service, if you wish. It's a bit unusual, but it can be done.
Are there alternatives to this idea?
Binding has nothing in particular to do with services being able to communicate with activities. Using some sort of callback or listener object via binding is a possibility, but it is far from the only one.
You can:
Have the service send a broadcast Intent, to be picked up by the activity
Have the activity send a PendingIntent (e.g., via createPendingResult()) to the service in an Intent extra on the command sent via startService(), to be used by the service to send information back to the activity (or wherever the activity wants it to go, such as a broadcast)
Have the activity pass a Messenger tied to its Handler to the service in an Intent extra on the command sent via startService(), to be used by the service to send information back to the activity
All of those work perfectly well between processes, as well as within a process.
You can use Android Interface Definition Language (AIDL).
You can find an easy to use guide here

using synchronous and async communication between Activity and Service on Android

I am currently writing an app, which consists of a service and an activity. The service is running in the background, doing some live audio processing. If the user want to get some information about the running service or want to change the settings of the service, the activity gets started and bind to the service.
Currently i am using the asynchronous messenger system to communicate between the service and the activity. For example, the service can send some results to the activity through a message and the activity can handle this message and show the results. This works fine, but it is stressful to write the messaging stuff for each communication. And it is not always needed. Sometimes i only want to ask the service, if a flag is set or not. If i do this asynchronous, i have to send a message to the service which asks for the value of the flag and the service has then to send a message back to the activity to answer the request.
So i want to have some getter and setter which can synchronously access the service. This can be done by using a binder, which works too.
The problem is, that i sometimes need synchronous communication to get the value of flags etc. and sometimes i need asynchronous communication to push the results from the service to the activity. So what i need is a binder and a messenger. But i dont know how this can be done, because the service can only return one object from the onBind() method, either a binder object or a messenger object.
Do you have any suggestions how this can be done or some other approach to realise asynchronous and synchronous communication between an activity and a service?
Thanks in advance!
Tobias
If you are already binding to the service, your activity can supply a listener object to the service, which the service will then call when events occur.
You just need to make sure that you unregister that listener object before unbinding from the service, and do both before the activity is destroyed, so your service does not wind up with a strong reference to a defunct activity.

How does Service synchronously communicate with Activity

My application contains a single main Activity and a service.
Service will run in background as long as possible even if main Activity is inactive, so I had to launch it with startService() instead of binding them together.
There would be many messages and data exchange between Activity and Service while running.
I'm planning to use broadIntent() to finish those communication, but you know, intents are all asynchronous. But sometimes synchronous communication is required.
How can I get them synchronously communicated with each other?
Thanks.
i suggest you to look for bindService and AIDL.This lets you open a communication channel between the activity and the service.
what about using LocalBroadcastManager, it will send broadcast from service to local activity only.
http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

Categories

Resources