Android --- Connecting activities and services
I have this basic design in mind:
- A main activity will offer a choice of sub activities and also create a Bluetooth service.
The Bluetooth service will reads and buffer live 开发者_JAVA百科data from a Bluetooth connected device. Enough data , at a fast enough rate (100 to 1000 sps ) so that I don't think it is realistic to use Intents or broadcasting
The sub activities will simply be displaying the same received data but in different way. Each sub activities will also the user to interact with the data in a different way.
- I really prefer that the Bluetooth service is agnostic of the Activity/View onto which the data gets presented.
I'd be willing to 'register' a bunch a 'destination' (which would really be activities) to which 'cooked' data would be sent to. I didn't quite get how to 'register' anything from starting an activity.
How do I pass, for example, a reference to my service to each of those activities? Or it might be the other way around; how do I register each activity to the running service.
Having a C/C++ background, I realize this might not be a good approach in Java. Thank you.
Ideally this not NOT the best way to approach it. Specifically android is a system that bases its services on passing information via intents from activity to activity, activity to service, service to service.
The way I would approach this would involving having an app on the phone that would be communicating with a service. Specifically the app would receive the data from the service. However; in order to allow the activity to listen to it, you could have the service broadcast a message saying there is new information, and have this activity intercept it. When the service is building this message, you could have information passed via an intent (extra) to the activity. The activity would have a Broadcast Listener inside of it that specifically updates information relative to that service inside of that activity.
And perhaps to keep information from being lost, throw messages into a stack and read it accordingly until its empty (This is assuming you get ton of information)
These links should help
Broadcast Reciever
http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/
Services
http://developer.android.com/reference/android/app/Service.html
http://marakana.com/forums/android/examples/60.html
精彩评论