I need help with something fairly basic I am having trouble understanding after a few hours of research and reading.
I have:
Service
-Thread
Method: write(string)
I need to pass a variable string from MainActivity to service and call write(string) when string changes. I started to implement a broadcast receiver in the main service but how do I call write(string) method from the main service? Do I need to put the receiver in the thread instead? I'm having a hard time understanding what I'm doing here. Please let me know if there is a better way.
The service contains all the Bluetooth connection and send/receive code. I plan in the future to have another service send data to the Bluetooth service to send to the BT device.
I am still a beginner at Android too but I think what you are looking for are Bound Services. There is also a related question here.
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 am writing here after countless hours searching the net with no gain.
I am trying to create an app which starts a service, this service will have a broadcast receiver in him which should detect when a phone call is received, than this receiver calls a method inside the service with the calling number.
Can anyone help me out here? what should i be looking for? in short how should i handle this?
Check out this question. Sounds like for you, you might simply use a singlton instance of your service, or perhaps a broadcast receiver. Definitely stay away from AIDL.
I get an error message when I attempt to bind from the onReceive() of a receiver to a local service before I drive a bespoke API on it.
"IntentReceiver components are not allowed to bind to services"
What is the best way of getting a piece of data from a service while in a broadcast reciever.
Many thanks.
P.S. I need to get the answer synchronously i.e. I have wait on the answer from the service so a callback may not be possible.
What is the best way of getting a piece of data from a service while in a broadcast reciever.
If the BroadcastReceiver is created from something else (e.g., an activity) and set up with registerReceiver(), then the "something else" is what should be binding to the service and doing the work.
If the BroadcastReceiver is a component registered in the manifest, then you need to rethink your approach to whatever problem you are trying to solve. These sorts of BroadcastReceivers cannot bind to services and cannot spend much time doing work. If the BroadcastReceiver cannot do its work in less than, say, 10ms, it should delegate control to a service via startService(). That service can then do the work on a background thread.
P.S. I need to get the answer synchronously i.e. I have wait on the answer from the service
See above.
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.