Android --- Connecting activities and services - java

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 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

Related

Android Conditional PendingIntent in Notification?

My application currently has two activities: a splash activity which displays the app logo and loads the user’s data from a remote database, and then the main activity that houses all my fragments. I’ve reached a bit of an impasse right now with notifications. I’m using FCM to deliver the notifications and have that working fine, but don’t really know what to do with the pending intents of the notifications. If the app is closed, I don’t want it to launch the main activity since I load the data from the DB in the splash activity. Thus the main activity would have missing data (it currently crashes due to null pointer exceptions everywhere). But if the app is still running, I don’t want the splash screen to be launched again. Is there some type of way to have a conditional intent baked into the notification?
Alternatively, is this just bad practice to do what I’m doing with launching the activities from the notifications? I’m by no means an android expert so there might indeed be a better way. I would prefer to keep my current activity flow however where the splash screen is responsible for the initial loading of data. I was hoping there was some way to just broadcast a message on the click of the notification and if any activities have an active listener then it would just consume that data.
The solution I have come up with is to have a single activity whose sole purpose is to process notifications and then pass them to the appropriate activity either through a new intent (if app is terminated) or through a broadcast (if app is resumed). Not sure if this is bad practice (would appreciate anyone telling me if this violates any type of software principle).
It's working like a charm so far and is honestly really nice to have all that logic in one activity anyway - all the service has to do is pass in the data from the notification and the action and the activity does the rest.

Creating callback to an activity from an AccessibilityService in android

I have created an accessibility service for my app where it would detect of the current outgoing call is answered or not but now i want create a callback function to my activity when the call is answered.
I have found some code to make callbacks from service in below links -
Callback from Service
RESTFul API
but i cant figure out a way to do that with my accessibility service, as i am not starting the service, android is, so there is no way to pass an object to the service.
If anyone can help please share.
Your service and your activity are separate processes. They cannot communicate directly. You should look into BroadcastReceiver. Note, though, that the user would have had to start your activity and have it running in the background. If the system decides it is low on memory and kills your activity completely, there is nothing you can do. You will not reliably get this broadcast with your activity in the background. You could try starting a background process from your activity for the sake of finding this event and storing the results somewhere for future consumption. Depends of course on your use case.

Android: How to trigger screen UI events (e.g incoming call screen) by a long running background service?

I'm quite new to android. I'm building an application that uses the android smack library to communicate with my XMPP server. I am able to send and receive messages perfectly however what I want is to run this ENTIRE sending and receiving messages code to a long term running process in the background and on receiving a certain message I want to launch a screen (something similar to the incoming call screen of android) no matter what the user is doing on his smartphone i.e. when the activity running the messaging service is not open (in a similar way when you get an incoming call from a viber contact and you get faced with the screen even though you obviously dont have the viber application open).
Any ideas on how exactly to do this ?
I have a few ideas in mind after research but dont quite seem to get the correct way and glue all peaces together.
Thanks in advance
you can use intentService to communicate with mainThread
here is a basic examples of intentservice and service
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/
service example
http://www.vogella.com/articles/AndroidServices/article.html
here is an official document
http://developer.android.com/reference/android/app/IntentService.html

Creating a messenger in Android

I'm (sort of) new to android development and I'd like to create an android messenger app based on the PircBot library.
There're several constraints I am aware of right now:
must use IRC, probably can't change this now since it's already in the requirement
IRC service needs to run in its own process so it doesn't interfere with UI while listening to IRC messages and it should receive messages in the background and send notification without having UI showing
I'm using different activities for different screens so all activities will need to use the service (logged in, joined channel, send/recieve message) and different activities should be notified based on the current state of the connection.
I've tried to bind the service from my activity but binder seems to only work with services in the same process. (I'm not sure on this one, search didn't return anything useful)
What's the preferred way to structure my project so that all the constraints can be satisfied? Also what other problems might I ran into?
Sorry for the bad english
IRC service needs to run in its own process so it doesn't interfere with UI while listening to IRC
I don't see any reason why the IRC service would need to run in its own process. A service, is, by definition, an element that does not interact with the user, as stated in the docs:
A Service is an application component representing either an
application's desire to perform a longer-running operation while not
interacting with the user or to supply functionality for other
applications to use.
And, as you said, by making this service run in its own process, it becomes impossible to bind to it (indeed, you need to do IPC communication at that point).
I'm using different activities for different screens so all activities
will need to use the service (logged in, joined channel, send/recieve
message) and different activities should be notified based on the
current state of the connection.
To satisfy this requirement, you can simply use the LocalBroadcastManager to share information from the service to all the activities. Your activities could have BroadcastReceivers for the events they wish to hear (connectionStateChange, MessageReceived, etc.), and would act accordingly.
Finally, because reading other people's code is always a good idea, check out these two similar projects, brought to you by your friendly search engine:
Android's SMS/MMS app
A simple IM app for Android

Service or Activity?

I'm struggling to decide which class to use, Activity or Service.
My application is complicated so I will use another example. A timer.
I would like to create an application that start to count the seconds when I open the application and keep counting even when I click the back button.
When I back to the application it will have a gui that show me the time.
So the gui I guess would be the activity and the counting method would be on the service.. but how do I manage that system?how they will be communicating? how can I do that?
You can use BroadcastReceivers and send broadcasts that your activity will be listening to, or you can use Messenger to send messages from the service to the activity
they can communicate with intents, or via shared storage, or content providers, ...

Categories

Resources