Android Services and UI update - android

I started learning android i've been playing with it and so far so good but i have some doubts about Services, i started learning them today so by gently if a say something very wrong.
For example, i want my app to grab some information over the internet from time to time, this polling period is defined by the user, then the UI gets updated. I though about creating a Service that run lets says every 30 minutes, gets the information and updates the UI.
If i get it right:
An IntentService just executes an operation and stops by itself sending the result through an intent(right?), so i think it's not what i want.
A Bounded Service is most likely used when you want IPC or allow binding from external apps, which again i think it's not what i want.
I think a Local Service is probably what i need, using a LocalBroadcastReceiver to update the UI, how can i make it to run the operation every X minutes( Handler postDelayed, ScheduledExecutorService or Alarm Manager ? )
If i understand it right a Service if not bounded can run infinitely if it's not killed due to low memory problems, making it a foreground Service is the safest ?
Last thing and it's kind of a noob doubt, if the user leaves the application(Click Home Button or opens other app) the app is still in background but the activities are in "Paused" or "Stopped" mode will the Service still be able to talk to them ?
Sorry for long post and thank you.

Your requirement : after every x minutes, start a service, pull some date, update UI.
Solution :
Define or set an alarm for every x minutes, to trigger a receiver.
From receiver start the service.
In the service, start an async task to fetch the data in doInBackGround().
Once data is fetched, from onPostExecute() send a broadcast to your activity.
In the activity have a dynamic receiver registered for broadcast sent from service.
From dynamic broadcast receiver update UI.

From what you've explained I wouldn't personally use a service.
The Android docs on services explain more but here is a snippet:
http://developer.android.com/guide/components/services.html
A Service is an application component that can perform long-running operations in the background and does not provide a user interface.
You could perhaps looks at using an AsyncTask, especially given that you only want it to run whilst the app is running:
http://developer.android.com/reference/android/os/AsyncTask.html
This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
There is a good answer here on how to run an AsyncTask repeatedly at specific time intervals: How to execute Async task repeatedly after fixed time intervals

Related

Scheduling a task to update UI when Activity in Background in every 10min

I am designing an app that has weather info to display at home screen. I want to update weather every 10 minutes.
I have a issue to update UI when home activity is in background (another activity is open upon home activity)
some help but issue in updating UI:
Scheduling recurring task in Android
Please advice me if another way to do this. Thanks in advance.
Use AlarmManager:
Periodically executing background tasks
Executes even if application is not running
Used when long running task is not required forever
For most cases, setup a Scheduler that triggers a background service at regular intervals
Suggestion: Use IntentService in conjunction with the AlarmManager
Reference & link: AlarmManager - CodePath
I have gone through your question and I am glad to give you the appropriate answer based on my personal experience.
Well if you are interested in gathering the weather updates every 10 minutes even when the application is in background, then I suggest you to use Background Service.
You can't imagine the advantages of background service. There are three kinds of background service
Started Service
Intent Service
Bound Service
All of the above three has there own usages. But in your case i suggest you to use the Started service. This service will start when you will call the startService() method, and will stop when you will call the stopService() method. Using the background service over Alarm Manager is recommended, and implementation is also quite simple and you can go through this link to understand the background service.This service will also keep on running in background even when your app is in background and will also gather the weather data every 10 minutes.

Android Services with Intent Services real time examples

I am getting confused between service and Intend service,what is the difference between service and Intend service,then on which kinds of situations we have to use service,Intend service any can explain with some real time example?
Thanks in Advance
A Service is a piece of code that will run on your main UI thread and will remain running until stopped, even if you aren't in the foreground.
An IntentService is a special kind of Service that starts its own Thread and queues incoming start calls to run on that thread one at a time, in the order they came in.
Use Service if you need a place for long running actions to occur that need to continue even if the Activity is killed. For example, tracking location via GPS for a maps app. Use IntentService for repetative pieces of work. For example, downloading files. Or syncing a database. You can think of IntentService as kind of like an AsyncTask that runs in a Service.

Long running background task, Android

I am parsing all text messages from the device and extracting words from them. For doing this I first used Service, but the issue with it was that it made application slower or sometimes I got notification that Application is taking longer to run.
Alternative to this I used IntentService. But problem with intent service is that whenever I stopped the application, I couldn't see my service running anymore. Alongside I also have to use Alarm Manager to schedule the things.
I am planning to use SyncAdapter for doing both of the things, but I don't think it would be a good option to use it. It would be really helpful if there is a better possible for doing this.
Background task might take upto 5-10 minutes for completion and I am planning to run it in every 12 hours. Though I won't be parsing old messages again. So it won't take longer after first time. The task should not end even when application is closed.
Basically IntentService is apt for background tasks which are not tied to the application lifecycle.
But problem with intent service is that whenever I stopped the
application, I couldn't see my service running anymore.
You can send updates to UI from intent service by using:
LocalBroadcastManager: how to use LocalBroadcastManager?
Handler: How to Collect info from IntentService and Update Android UI
Also you might want to see this video: The Zen of IntentService. (Android Performance Patterns)
EDIT:
Forget about using IntentService, it stops as the app stops because it runs on the same process as the app.
Since you want your service to work as a job every 12 hours, you could use a 'Scheduled Service'.
You can use JobScheduler or Firebase JobDispatcher API

How can I create a never ending IntentService?

I use a service to continuously synchronize information to display on the activity. The service runs an endless loop while(true) in which the information is updated every 10 seconds. In some devices the service stops after a time of execution. How I can keep the task of the intentService running? It must run even if the user minimize the application.
You shoudn't do that (and you even can't since android 6.0: doze). Consider using cloud messaging to notify your app that something has changed on the server.
A Service is ideal for hosting long-running processes that outlive any one activity. If you're just displaying the data, as opposed to saving it or doing some kind of background processing with it, there's no reason to use a Service at all. Just use Handler#postDelayed(...) in the activity, and make sure the task is removed on pause.
Polling every ten seconds is probably excessive. In fact, polling at all is probably inefficient, unless you expect the data to change as frequently as you are polling.

What to use: service or threads

I am developing an android app which fetches/uploads data from/to the web service every n minutes. This upload/download is only done when the app is running. But this might change in future.
I dont update the UI when the new data is downloaded. The UI is only updated if the user is on the current screen(app have multiple activities)
My question is what is the best approach to this problem.
I dont think service is the right approach as it sounds like an overkill(in the present scenario). AlarmManager could be an option.
Running threads inside a service be an option ..something like this .
Any pointers/suggestions would be great.
Thanks
I am using AsyncTask in my activity to ask .net web service some information and it works and easy to use.
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
Well, in this case, since the app would already be running during the time, either would work great, but a service can be called from anywhere within the application so this is where I would use the service over the thread.
If you want to create the thread to only be used in lets say Main.java, then thread would work fine, these are the only things that I can see really making ANY difference at all, they're really pretty close, and in this case neither gives a distinct "correct" answer, but I would choose Service
I think all approaches you noted would work ok. Personally I'd go with this:
Use AlarmManager to wake download service. Start it when Activity is shown, stop it when activity hidden.
Download service should be short lived: start it to do the upload/download and then shut it down.
If download service does get some new data, it sends a Broadcast which Activity listens to.
Just broadcast a message after your upload/download is done, and then have a receiver start the service and then have that service stop itself. And you are done.
This should be done if you dont plan on polling the server for new information or anything. Primarily this kind of approach would be for onetime update, interpret, finish. And wait until the next update. Which primarily for most cases is streaming.. but depends on what you are getting.

Categories

Resources