today I was learning about Intents and binding an activity to service, I found it quite easy and great way to transfer data from the service to the activity. I wanted to test out an activity that would start a service, this service would call on a webpage to get some data. I binded the activity to the service so that the activity could call on the service method and then toast the results. In the service method I call on a webpage and return the data. When I tested it I was getting a null pointer exception, im pretty sure I am not doing it the right way , does anyone have any suggestions how to implement this properly.
You're dealing with some fairly solid thread synchronization issues here. You would be far better served registering some kind of listener paradigm such that you register your Activity as a listener to the data that your Service is producing. As it currently stands, there's no synchronization between your Service and Activity at all, resulting in the Activity trying to fetch the results from the Service before they're ready. There are a ton of resources out there discussing the listener pattern or Observer pattern.
I should note, I have no idea whether your Service is actually pulling things down correctly. I'm just looking at your communication between the two.
When returning results from service , its better to implement ResultReceiver interface in you activity and pass receiver object to the service via putExtra. In your service fetch the receiver object and call receiver.send() function to send anything in Bundle. [I have tested this pattern in IntentService at least].
Edit:check out this post for complete implementation.
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 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
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 have a service running in the background. Based on some condition it has to start some activity. Activity has to send back the response.
I did google search and found out we have to use Notification mechanism. But I am not clear how to send the response back from activity to the service running.
Also service is collecting sensor data(acclerometer, gps). So should activity be started in separate thread so that collecting sensor data is not affected.
Please clarify.
Activity would be started in main UI thread, instead, your long-running service should run in and manage its own thread, since according to the document, service is also created in main thread.
Basically the best way to communicate is to use Intent. This allows loose couple of sender/receiver (i.e., activity/service in your case). Intent is a large topic in terms of android, and yet it is one of the most fundamental one, I think you should look for tutorials online about it.
If your activity is opened and return the result after completing the task of Activity then you can use startActivityForResult and then return the result.
Using of notification is simple, just create a interface class, and implement the class in your service. When you need to send data back to service, you can just call the appropriate method with data.
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.