Need Android background service - android

I am building an Android application.
I am trying to do a running app. this means I will need a background service running and counting my GPS data and report that to the activity.
But - I want the activity to be able to send actions to this service too (Start, Stop , etc).
I have struggled with a few methods of doing this 2-way service<->activity communications.
Any reference? what is the best approach to do that?
Thanks

Did you try service bind mechanism?
You can create service for collecting GPS data (http://developer.android.com/guide/components/services.html), start it on application startup (http://developer.android.com/guide/components/services.html#StartingAService), bind this service on starting your activity and use binder object for transferring data between the service and activity (http://developer.android.com/guide/components/bound-services.html), unbind on closing your activity and service will continue working on background.
In this model, your service will work always and will transfer data when it needed.

Binding might be an overkill when you deal with the same process. I would recommend using Otto from Square as an event bus mechanism to post events to your service and vice versa.
If you dont want to add a library to your project then consider using the LocalBroadcastManager that is part of the support library.

Related

Update UI from a bound foregroundservice

Since I am quite new to developing an Android app I would like to ask the more experienced developers how to approach the following problem.
The app that I am trying to build is supposed to do the following:
When the app is running it is constantly receiving various doubles via upd packages. Depending on the double values it will show certain messages in text- and image views on the main activity.
So far I already set up a foreground service (running on an extra thread) which is receiving the doubles. The service is bound to the main activity.
I decided to use a bound service because in future I would like to connect additional activities to that service.
And here are my questions:
Did I choose the right config (bound foreground service) for what I am planing to do?
How can I update my image- and text views depending on the values of the doubles received by the udp service? Can I do this direclty inside the service and only push an update if the values changed?
Thank you very much for your time and assistance on this matter.
Did I choose the right config (bound foreground service) for what I am planing to do?
No objections to the bound Service. Having an interested Activity bind to the Service will ensure it's being kept alive as long as the Activity is considered to be in the foreground.
How can I update my image- and text views depending on the values of the doubles received by the udp service? Can I do this directy inside the service and only push an update if the values changed?
You should not try to update a View "from outside". Only the component (Activity or Fragment) to which the View is belonging should have the right to modify it.
Instead, just notify interested components of changes in the data.
Ideas for intra-app communication:
You could send broadcasts via LocalBroadcastManager
Or you have the Service update a data repository and use LiveData to keep the UI up to date like described in the Guide to app architecture .

Control an application from a service

I've written an application which has multiple activities with multiple buttons. A colleague of mine wrote a service which detects gestures, based on the gyroscope sensor (move up, down, rotation...). We want to join the application and the service so we would be able to control the app using gestures from the service.
Since we both don't have so much experience with Android, we'd like to ask, how this is usually achieved?
For the Service -> Activity communication you can use LocalBroadcastManager, check this SO answer: Android update activity UI from service
For the Activity -> Service communication you either start service and add extras to intent or have a bound service.
Make sure you read this article: http://developer.android.com/guide/components/services.html

Android - Can an activity and a service communicate using the Observer design pattern or similar?

Is it possible to make an activity and a service communicate, using the Observer design pattern?
I want to make them communicate both ways by giving them both the role as Observer and Notifier
The reason why I want to do this, is that I want low coupling between them. So if the activity crashes for some reason, the service will still be running and still trying to notify the GUI without crashing.
The reason I want the service to remain running, is that it acts like a server in a LAN and I still want the system and the clients to communicate even when the GUI of the server is gone.
If this can't be achieved using the Observer pattern or is too complex, is there another way to achieve what I described above?
Thanks in advance
You can run the service as foreground if your are using notification . so when the activity exit the service can update with the notification or remote views.
Also , When you start the activity you can bind the service from the activity to communicate using service connection.
bindService(new Intent(this,
YourService.class), mConnection, Context.BIND_AUTO_CREATE);
A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)
check the link - http://developer.android.com/reference/android/app/Service.html
http://developer.android.com/guide/components/bound-services.html
You could have a communication from Activity to Service with a usual Binder. The Service can inform the Activity about changes with a Callback, which you can register in onServiceConnected() and unregister in the Activities onPause().
With this you have a two way communication between Service and Activity and the implementation isn't to complex.
So if the activity crashes for some reason, the service will still be running
The thing is, Activity doesn't crash. The whole app process does. So if you want to separate Activity and a Service, you need to run them in defferent processes. Note sure about if Service is restarted after process crash, though.
Take a look here https://stackoverflow.com/a/12022185/468311, that would be a good start for you.
If this can't be achieved using the Observer pattern or is too complex
As suggested, use Binder. Or you can communicate using Intents.
But keep in mind that Service wasn't intended to be used as a forever-running piece of your app. Try avoding this. Use Service for pereodic background operations.

Service or Bound Service?

I'm creating an application that connects to an XMPP server on Android. I want to keep the connection on till the user logs out.
Should I use a regular Service or a Bound Service to keep the connection on?
Any tips, advice and helpful information are welcomed.
I like this explanation:
Started services are easy to program for simple one way interactions
from an activity to a service, however, they require more complex and
ad hoc programming for extended two-way conversations with their
clients.
In contrast, bound services may be a better choice for more
complex two-way interactions between activities and services. For
example, they support two-way conversations.
So, as you said, If you want to interact with the service use bound service. With started services (or intent services) you could do it, only it would require more complex programming.
(by Douglas Schmidt: https://www.youtube.com/watch?v=cRFw7xaZ_Mg (11'10'')):
Here is a summary that helped me understand (thanks Doug):
Finally, one last link that helped me also:
http://www.techotopia.com/index.php/An_Overview_of_Android_Started_and_Bound_Services
Started services are launched by other application components (such as an activity or even a broadcast receiver) and potentially run indefinitely in the background until the service is stopped, or is destroyed by the Android runtime system in order to free up resources. A service will continue to run if the application that started it is no longer in the foreground, and even in the event that the component that originally started the service is destroyed
A bound service is similar to a started service with the exception that a started service does not generally return results or permit interaction with the component that launched it. A bound service, on the other hand, allows the launching component to interact with, and receive results from, the service.
A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.
If all the code exists in one activity from user connected to user logout then go for bound service
But if it is code exists in multiple activities try with service
I found out the difference between the two and when to use it. If you want to interact with the service (for example send arguments etc), use bound service and it return the service object in the onServiceConnected method (where you can call methods in the service). You cannot interact with a regular service (from another class)

Android - Shared networking between activities

I am developing an application which utilizes a custom network protocol over TCP. Several tasks within the application use different activities however each activity requires the networking enabled (since each activity has to send/receive some custom packets over the network).
So my idea is basically to
At application start - create a service to handle networking ( I read somewhere that I should do the networking in another thread in this service to prevent ANR)
When a new activity is run, it binds to that service. During that time it sends/receives data from the service. When the activity ends, it unbinds from the service.
I would like to know if this makes sense.
Also, I understand that I can send data to the service (to send over the network) using the aidl interface, but how would I receive data from the service? Polling through the aidl seems to be the only way I can think of - which means I would need another thread in each of my activities to prevent ANR. Surely there is some better way.
I appreciate your help and am open to suggestions.
Actually I've been reading more and am beginning to look at callback methods in the sample code provided here. After reading it more thoroughly I have a better understanding of the services and AIDL.

Categories

Resources