So basically what i am trying to do is i want a service that runs in background and updates the LatLong to the server. This operation needs to happen all the time even if the application is running or not.
Now when the application is launched i want the service to calculate the distance between the latlong and update the UI in addition to the work that it was already doing i.e. updating the latlong to the server. i want the service to do the additional work for multiple activities. lets say i launch the application and i am on Activity A, onclick of a button on Activity A the service starts updating the UI and when we click gain it stops updating. Now i am on Activity B and on click i want the service to do some work in addition to the updation of LatLong and update the UI.
What would be the best approach to achieve this.??
EDIT
The problem i am facing is not getting the service update the UI but making the activity communicate with the service when it has already started.
i can pass on some data when i am starting the service but how to communicate with the service when it has already started. How to tell the service that see you are already running and doing some operations now you have to perform some more operation on top of the previous operation.
I can make some static method in the service and call them when i need to perform the extra operation but i dont wanna do that.. i want to better approach.
Here is the basically services runs on the same main thread process as ui. When you want make it run continuously you have run it in your tread. In this way you avoid service being get stopped as application go in background or killed. (Please take look at Service.START_STICKY flag, this is what you need as i guess).
And more coming your second problem of activity getting updated with service information or data that is being collected for this you need look at " How Bind the service". (In activity check for service connection and Binding to a activity, Unbindibg is also there have look at it also). Activity has all the call backs for it you need to implement binder.
EDIT
Service to update ui has to have send and receive intent mechanism. You can broadcast and intent from the services check your activity is running or not. If running broadcast intent and have receiver in activity to listen it.
You can do it by interface mechanism too
Related
There is some long processing that need to be completed, so I put it in a service. The activity must be able to connect to the service, show the user current results from the service. So I start the service with start Service and later call bind Service (with BIND_AUTO_CREATE) as in LocalService from http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle. I want it to run until its job is done, and then self stop, even if client is still connected to it. (or determine the client to unbind) Is any way to do it with the sample LocalService?
I was considering passing a handler to the service so that it can send messages back to the activity, but I don't want the activity to get leaked. I am just getting used with services, so maybe I am misusing something.
EDIT: The workload consists of several threads, synchronized and run in parallel, so I guess is not a good candidate for intent service. Some data analysis is done in background service, and when the user restarts the activity that started the service, it should display some graphics according to current values computed by background service. All background processing is triggered at the beginning, and need only inspection later on, when activity connects to it. Android should not be able to stop the service. When the job is finished, the service should be able to terminate even if the activity is connected to it.
I just recorded a callback with the service. If the activity is not connected to service, it sets the callback to null. In this callback I call stopService() and then finish() on the activity. I am not sure that it is the best method, but it works fine for me.
If you want a service to be stopped when it is finished, I think what you are looking for is IntentService, they work as services, but run in another thread and when they are completed they dissappear.
Check this out
EDIT: NickT link is better, check that out! :)
I am implementing a Service which will start when MainActivity starts. This service checks for and maintains a network connection.
I have not bound the Service to the Activity because I want the service to continue running even if the activity isn't available. The Service will receive messages from the server that it is connected to.
I am struggling to choose the best logic to do the following when the service receives a message.
Check if MainActivity is currently open and in front of the user
If it is call some methods in the activity to interact with the UI
If there is no activity update the notification are.
My question is;
How do I correctly check if the activity is running in the UI from my service? I know that I could bind the service but I wouldn't want to unbind it if the activity is closed. Would that be a problem?
I will somehow need to send messages from the service to the activity. Given the above scenario what would be the best way to do this?
Do it differently.
If your Service does not run in a separate process from your Activities, then your Service can provide a synchronized (multithread-safe) list of messages via a subcalssed Application object, where your Activity can look it up. However, this would only be best if the polling occurs on certain other events.
If you want to sort of "push" the message to your Activity, your Activity should register with your service upon finding out that it runs, not the other way round. In this scenario, your Activity should implement an interface through which the Service can notify your Activity of new messages in a Thread-safe way.
Of course you could also go straightforward and simply post notifications which open an Activity, but as I understood it, you want to provide a more smooth integration.
An idea would be to let your Service send status bar notifications when a new update is available.
Implement the notification such that when clicked, to open MainActivity.
Then, when the user will click on the notification, if the MainActivity is already running then it will be brought to front, otherwise it will be started.
So this basically eliminates the need to check if MainActivity is currently open in front of the user (which I see as a bad practice).
I'm about to develop an application for Android.
I need to continuously run an update thread that executes a task in a given interval. And this thread needs to be shared between activities.
I would also like to notify the active Activity when the thread executes a task.
In iOS I would execute a thread from the AppDelegate and then send notifications, but how do I achieve this with the Android SDK?
I'd say create a Service that does the work for you. When it starts or finishes (or at any point you want), you can send a custom broadcast intent to indicate all parties that your service has passed this point. See Intents.
If you want to start this Service periodically, also have a look at the AlarmManager. That allows you send a broadcast intent periodically - in this case the intent that starts the service.
Also note that it's usually wiser to to terminate a service via stopSelf() when it's work is done. Run it again via intent when needed. Keeping it alive all the time isn't a good idea when it comes to battery life.
Make a service and implement a thread with infinite loop and from there you can do your job....to update data in activity you can make a static method and you can call it from there with appropriate arguments....
I've created an application which does synchronization with the server.
I've already have a code, which does synchronization once user pressed a button. Now it is time to add Service there.
I have the following questions with regards to the services on android:
will the service be started if user never run application before? (i.e. just installed that)
when the service will be started first time? can I start it from onCreate of the main application?
if user presses Synchronize button in the application, should I start that Service or should I have different process for the same? How can I check then that background synchronization is not happening at the same time?
should I use ASyncTask, even if the service is started as
startService(new Intent(this, ServiceSync.class));
The preferred approach to syncing data in Android is providing a SyncAdapter to perform the sync. You have a very nice summary of the required steps in this post.
You will also find these articles useful.
Edit:
1-2-3: You can use a bound service to manage the interaction and service methods from your activity. You have full working samples in the link provided. Basically, you bind to the service in your activity's onStart method and unbind onStop.
Remember that a service runs on the UI thread, so every time-consuming task such as fetching data over the network must be done in a separate thread.
4: You don't need an AsyncTask here.
I have a service running in the background. Based on some condition it has to start some activity. Activity has to send back the response.
I did google search and found out we have to use Notification mechanism. But I am not clear how to send the response back from activity to the service running.
Also service is collecting sensor data(acclerometer, gps). So should activity be started in separate thread so that collecting sensor data is not affected.
Please clarify.
Activity would be started in main UI thread, instead, your long-running service should run in and manage its own thread, since according to the document, service is also created in main thread.
Basically the best way to communicate is to use Intent. This allows loose couple of sender/receiver (i.e., activity/service in your case). Intent is a large topic in terms of android, and yet it is one of the most fundamental one, I think you should look for tutorials online about it.
If your activity is opened and return the result after completing the task of Activity then you can use startActivityForResult and then return the result.
Using of notification is simple, just create a interface class, and implement the class in your service. When you need to send data back to service, you can just call the appropriate method with data.