Accessing a service/thread from different activities - android

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.

Related

Android activity - remote service aidl two way connection

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

Is binding to a started local service required?

I want to use a started (foreground) service to manage a network connection that should persist when the user leaves the application for a short time, and that the user should be aware of (so he can return to the app and maybe disconnect). This service will only ever be used locally by activities in the same process.
Maybe it's just because I am new to Android, but I find it unnecessarily difficult to bind to this service in every activity that uses it - in particular, the asynchronous nature of binding, which only really seems to be necessary for accessing services in a different process. Is there any indication against just accessing the started service through a static variable instead?
Maybe I'm understanding your question wrong, but there is no need to bind to the started Service from every Activity. Instead, you could simply start the Service from wherever you need to interact with it. This calls the onStartCommand() if the Service is already started. You could include an extra with the Intent that starts the Service to distinguish between the first start and subsequent ones.
Of course - this addresses the use case where you do not want to have a client-server mode of interaction between your activities and the Service - that scenario requires binding and if you really need binding, then you need to bind from every component that needs to be served by the Service.

Design approach to use in Android when several activities need to connect to a service

I have a class that starts a Bluetooth reading thread and another that receives/decodes what's read from that port and produces some output logs depending on the information read.
In my design, those 2 components form a service for my application (multiple activities) from where I would like to start/stop getting the output logs on a continuous basis (typical frequency of 2-3 logs per second).
My questions:
1) Should I derive from Service or IntentService. The doc says about IntentService: "This is the best option if you don't require that your service handle multiple requests simultaneously". This may be my case since the main activity will start/stop the service...
2) What would be the appropriate way to catch the service events? Does the BroadcastReceiver is appropriate for this type of communication?
3) I may need to occasionally send some stuff to the Bluetooth port. So, I'll have to pass information from my application to the service. Does the PendingIntent should be used for that?
Thank you!
Should I derive from Service or IntentService
IntentService is designed for discrete tasks, not stuff that would run indefinitely until the user manually stops it. I would use Service.
What would be the appropriate way to catch the service events? Does the BroadcastReceiver is appropriate for this type of communication?
That is certainly one approach. You might use the LocalBroadcastManager from the Android Support package to reduce overhead and keep everything private to your app. Have your activities register a receiver in onResume() and remove it in onPause(). The foreground activity will then be notified of events.
I may need to occasionally send some stuff to the Bluetooth port. So, I'll have to pass information from my application to the service. Does the PendingIntent should be used for that?
No, I would have the activity simply send a command to the service via startService(), with the data to be passed included in extras on the Intent. If you have data that cannot be packaged as extras, you may need to consider binding to the service, so you can get a richer API, though this makes configuration changes more annoying.

Best way for Service that starts Activity to communicate with it

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.

Passing data from a service to a non activity class/object

I currently have a service running in my Android application that makes a wifi scan at a set interval and returns the results.
I was then passing the data using an aidl interface and to do this I had to bind the service to my Activity I was passing the data to.
However the design has changed and I now need to be able to send the data that the Service returns to just a normal class that does not extend an Activity.
So I want to pass the data from the service just to another class that doesn't extend Activity and then be able to use that data in a method of that class.
So I was wondering how I would go about this if it is possible?
EDIT:
Actually is this even the correct way to implement the following:
I have an application that will have no User Interface (not exactly none altogether but VERY minimal).
I want it to scan for Wifi access points at regular intervals, check the connected state of Wifi and check the connected level of the currently connected access point.
It also listens for the OUT_GOING_CALL broadcast and traps the call if connected to wifi and then pops up a dialog that tells the user the call has been blocked and offers the user cheaper options than Gsm if there are any.
I currently use the Broadcasts to get the Scan Results and to get changes in the connected Level of the wifi connection also.
However I had thought that moving these to a service of my own would be a good idea, however would I be better leaving them as Broadcast Recievers in Non Activtiy and Non Service classes and just make the main Engine of the application a service that turns on and off these broadcasts from there Classes (Objects) ?

Categories

Resources