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, ...
Related
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.
I have an intentService in my app that I use to play music files in the background.
In this service, I have several methods (play, pause, stop, etc.) that I need to call from my main fragment in response to UI input.
However I cant seem to find any way of sending messages to an intentservice, I can only find ways to sending messages from an intentService to an activity.
If someone knows a way I can achieve this, I would much appreciate it.
Im developing for an android aplication and I need to run a thread in the background that is going to be check the current gps and show an activity depending of the current location data but the application have diferents activities, and I need this thread running all the time no matter if I change between an activity and other.
Can I do that ?
I know that I can add threads to an activity but I dont know how have a main thread in the background
You need to use a Service which is not tied to any particular Activity. You can read all about Services here
In your case I ssuggest you take a look at IntentService which automatically generates a background thread for you
You want to use a service here:
http://developer.android.com/reference/android/app/Service.html
You can run things in a background thread if you want in the service. And then you can bind to it within your activites to communicate between the service and activities.
I have been developing Android application that use Activity with "download" button and Service for executing downloading in the background. And I have following task: to show message about downloading if application is currently displayed. How can I detect it? Is there standard Android OS functions for it? Thank you.
You can do this at the activity level, not for the application as a whole. Check out the description of the activity lifecycle. (Also see "Managing the Activity Lifecycle" in the Activities framework topic.) When your activity becomes visible, the framework will call it's onStart() method. When the user can interact with the activity, the framework will call the onResume() method. You can override one of these to know when your activity is showing or actually interacting with the user.
Note that, as described in the documentation, things work a little differently starting in Honeycomb (3.0).
The basic method is to use Broadcast receiver to share information between a service and an application. If your requirement is to update the status/progress of download on the main activity, you can use it.
Here is a simple tutorial about how to share information between a service and an activity. You can update the progress of download. It works for the activity that is currently running.
To check for it as the activity starts, you should override the onCreate, onStart() or onResume() functions.
Also there is a very neat solution presented in another question and i recommend that you should try to use that. It involves extending the Application class to store the information, and use its instance to check for updates. See this
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