I need to upload data to the server repeatedly (say after every 10 minutes). The application will check in the local SQLite DB if there's any unsynced data and will upload it.
If I call an AsyncTask from a Handler repeatedly, will it work even when the app is paused (user navigates to another application)?
How can a Service be used to do this (as service can be run in the background)? Should I use Service or IntentService?
An AsyncTask can run after the calling app is destroyed, however, if it calls onPostExecute() it will crash the app if this method updates the UI. Handlers will also continue to run. However, the JVM process may be killed off at any time. AsyncTask is only supposed to be used for short tasks lasting a few seconds.
A Service is not married to an activity and can outlive it should the app be destroyed. This is where you should be updating your server.
A good tutorial: http://www.vogella.com/tutorials/AndroidServices/article.html
If I call an AsyncTask from a Handler repeatedly, will it work even when the app is paused (user navigates to another application)?
When Android kills an app, can a function be stopped halfway?
"AsyncTask may continue running but may or may not to be fully functional"
How can a Service be used to do this (as service can be run in the background)? Should I use Service or IntentService?
I guess it is better to use service to do this. (i am not so sure anyway)
What is the difference between an IntentService and a Service?
Related
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.
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.
My app is synced with data that is received using an asynctask. When data is received I update a listview and generate a notification. It works great but in case the app is in pause mode, I want only to generate a notification. I want it to continue executing this asynctask even after onPause (if the user switched to another app or pressed the home key).
I read a lot of posts here about how to repeat an action but never saw a reference to what happen when/if the app goes into pause mode.
Why not start a service that runs a background thread?
The service will continue running even if you are not using your app.
When the onStop() of your main activity is called, start the service.
The thread in the service sleeps and every so often connects to the server and checks for updates.
In the onCreate() of the service, start the thread.
In the onStartCommand() (which is called if the service already exists) of the service, check if the thread is alive. If not, start the thread.
Guide for creating services: https://developer.android.com/guide/components/services.html
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
I'm developing a countdown timer app for Android. The app can show the remaining time. It has to be working even the phone is locked or the main activity is destroyed. I was wondering which of the following should I use and, more importantly, why should I use it (and why not the others).
Service
Runnable : A static reference to a Runnable object. The reference is declared in an Activity class.
AsyncTask
In my opinion, I would use a service if your application needs to be able to notify user in background, so if your application has been put in background by the user, you can still notify the user when you timer is ready, and then can use location based event time, etc.
A Service would be best in this situation, it would, as you require, carry on running even when the main activity is destroyed and would be less prone to being killed by the Android OS.
Both AsyncTask and runnable would rely on your application still being alive, whereas a Service runs independently and you would bind the Activity to it when it is opened or re-started to show information passed from the service, or the service could on it's own, display notifications and so on.
You can use Handler, Check here
http://developer.android.com/resources/articles/timed-ui-updates.html