I have two problems:
I know that for connection activity and remote-service I have to use AIDL.
I tried this and it's work but I can find only one way connections example. In simple words - reading something from service (by activity). But I need solve for sending some data to activity (by or from service). It's so important because the service have to send some information to activity immediatly after some its events (obtain data from the net).
Is it way to bring to front again closed application (activity) from the remote service?
Any suggestions would be greatly appreciated.
Regards
Artik
It's so important because the service have to send some information to activity immediatly after some its events (obtain data from the net).
You can use AIDL for two-way communication. You would need to expose not only the service interface, but a callback interface, via AIDL, with the client having the .Stub of the callback and supplying an instance of it in a parameter to a method on the service interface. This gets a bit complex -- here are a pair of sample apps from my book that demonstrate the technique:
Service
Client
Is it way to bring to front again closed application (activity) from the remote service?
Your service can call startActivity(), but generally that is a bad idea. The user may be in the middle of doing something else, when all of a sudden your activity pops into the foreground. Occasionally, the user may deem your activity to be more important, but not always. Consider using a Notification instead, to let the user know that there is something in your app that needs the user's attention.
First, create a private resultreceiver variable in your service. Then create a method to set this resultreceiver via a connected activity. Then use AIDL to pass on a resultreceiver to the running service from the activity via the method you just made. Then in the service use resultreceiver.send if the resultreceiver is not null.
A few examples to get you started
http://lalit3686.blogspot.com/2012/06/how-to-update-activity-from-service.html?m=1
http://chrisrisner.com/31-Days-of-Android--Day-28–Intents-Part-3--Service-Intents
Related
I am developing a chat client in which I have a Service which is listening continuously from XMPP server. I have few questions regarding the architecture of this service.
I have read in the documentation of Service class on Android Development Page that onCreate() will be called once when the service run for the first time thats why I have written the connection to the server code in the service onCreate() method. Is it OK?
When I click on a ListView element which is the name of my friend in my client activity a new activity will open which will bind to the service for listening and sending chat messages. I want to know how a service can differentiate that a specific message is for which activity? because If I am having chat with more than 1 friends there will be more than 1 activites and the service has to differentiate that which message is for which activity. I need to know how can I implement this mechanism ? It could be very helpful if there is any tutorial for this or describe what do I need to do to implement this mechanism?
Is it OK?
That is impossible to answer in the abstract. There is nothing obviously wrong with that approach.
I need to know how can I implement this mechanism ?
To be honest, that UI sounds... awkward. That being said, you can have the activity supply a listener or callback object to the service, which the service uses to route messages back to that activity.
I'm writing a Bluetooth remote control application to control my Bluetooth enabled robot, but I have a hard time understanding the workflow of an Android application. I know what I want, but it's not very easy to do. Other responses haven't been satisfactory.
Here's a rough application layout I want:
If you have a better idea of how to do this thing I'd be happy to consider.
Mainly my problem is accessing the connection thread/service (whatever the name is) from the connect method of the main activity and from the control activity, how do I pass the reference? I know that the main activity can disappear when in a sub-activity of the application, so I'd have to pass the reference by getExtra() probably, but it only takes a String variable...
One method you might like to consider is using a Bound Service. This can be made to return an IBinder interface, allowing access to the service's public methods. You can make any of your activities bind to the service and thus pass/retrieve data to/from the service.
If you need the service to communicate with the activities immediately (e.g for lost connection), then the service could use sendBroadcast to inform activities which had registered a BroadcastReceiver to listen for the intent sent in the broadcast.
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.
I'm working on establishing a two-way communication between an Activity and a Service which runs in a different process.
Querying the process from the Activity is no big deal. But I want the process to notify the Activity on events. The idea behind it is this: the service runs independently from the actual app. It queries a webserver periodically. If a new task is found on the webserver the process should notify the activity.
I found this thread over at AndDev.org but it doesn't seem to work for me. I've been messing around with BroadcastReceiver. I've implemented an interface which should notify the Activity but the problem is that the listener is always null since the Broadcast from the process is done via Intent, hence the class that extends BroadcastReceiver will be newly instantiated.
How can I establish a 2-way communication? This has to be possible.
Thanks for any help,
steff
Either use BroadcastReceiver or have the Activity register a callback or listener object that the Service calls on key events. The links above are to book example projects demonstrating each of those techniques.
I think you should have the BroadcastReceiver start your activity again with the result in the Intent.
Or you could use AIDL about AIDL. The samples also have an (multiple?) example how to use AIDL and services. But AIDL might be to much of a hassle for your purpose.
You have to use BroadcastReceiver to receive intents, and when you want to communicate simply make an Intent with appropriate values.
This way you should be able to make a 2-way communication between any component.
I have a service that listens to a socket. When receiving certain input it is to create an activity. When receiving other input, it is to kill this activity. I have struggled for a while to make the service communicate with the activity through AIDL (http://developer.android.com/guide/developing/tools/aidl.html), but this seems to not be effective. I think AIDL is only effective when the process that is to be talked to is a service, not when it is an activity? I would love some directions or suggestions on how to solve my problem.
Cheers,
I have a service that listens to a
socket. When receiving certain input
it is to create an activity.
Please make this configurable. Services should not be starting activities except in very unusual circumstances (e.g., the socket is a SIP connection and you are creating a VOIP client). Popping up an activity interrupts the user in whatever they are doing.
When receiving other input, it is to
kill this activity.
The only scenario I have seen where this is a valid pattern is dismissing the in-call screen when the other party hangs up the line. If you are creating a VOIP client, your proposed pattern should be OK, but otherwise, please reconsider having the activity vanish in the middle of the user using it.
I think AIDL is only effective when
the process that is to be talked to is
a service, not when it is an activity?
No, it works in the reverse direction too, but usually only if the activity is the one starting the service and binding to it. More importantly, AIDL is only for cross-process communication.
I would love some directions or
suggestions on how to solve my
problem.
You have not really provided enough information on the nature of the communication to give you a thorough answer. What, exactly, is the service trying to tell the activity? Is the activity also trying to communicate with the service?
The recommended pattern for ongoing communication from an activity to a service is to use the local binding pattern. You will find an example of this in your SDK samples, and you can find one here as well.
The service then has options for communicating back to the client: via a callback (e.g., the Handler in the answer supplied by Mr. Smiljanić) or via broadcast Intents. In the case of the callback, the activity would need to bind to the service in order to get access to an API to provide the callback object. The service would then hold onto that object and call methods on it during key events.
If your service is doing its primary work on a background thread, you will need to ensure that your UI operations get performed on the UI thread. The Handler is one approach to that.