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
Related
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?
I am working on a app that during boot time starts an activity that logs in to my server (needs an activity to log in through facebook) using a service (initiated with startService). This service establishes XMPP listeners and does nothing after that, just waits for connection. I want this service to run all the time the device is up.
The problem is that the activity stops after a while and my service is also stopped. The service returns START_STICKY so I was expecting it to hang around. Also the service doesn't do anything except wait for connection.
The activity has the properties:
android:excludeFromRecents="true"
android:noHistory="true"
android:launchMode="singleInstance"
so that it does not show up in the task list (when user long presses the home button).
The activity is stopped when the user long presses the home button and the service also ends. I am thinking its possible that the application exited, that's why the service also ends. I could not find any way to keep the activity from not stopping. Maybe its stopping because of the above properties.
So what can I do to keep the service running all the time. How can I keep the application from being removed. I read somewhere that if I keep a while loop running in the service then START_STICKY can keep the service around??
I can use AlarmManager to start the service but I don't want it to stop easily and then have to restart it every time.
I don't want to run a foreground service. I can not run the service in a different process since I am using existing code that does not do IPC. Any help will be appreciated. Thanks.
There are two things to keep running a service indefinitely; create the service using startService() and return START_STICKY from onStartCommand(). You seem to be doing this both. With these two steps, the service may be shut down by the system but it should restart almost immediately.
The only suggestion I have is to create a separate thread in the service. This is because by default, started services run in the application main thread. If the service is constantly doing certain task, it may block the main thread and kill the application. Google doc has an example of implementing this:
http://developer.android.com/guide/components/services.html#ExtendingService
I am new to IntentServices. I am using My IntentSevice to perform background webservices call. My application requires the user to sign in...
when the user signs in, the IntentService is started and in the onHandleIntent method I declared a TimerTask that runs every amount of minutes (please inform me if its the best solution to periodically perform a task in IntentService).
My question is, how can I stop the IntentService from periodically executing if the application was closed or the user signs out.
what I tried:
when the user signs out, i am cancelling the timer task. and so the webservices calls stop, but i don't know how to properly do that in case the application force quits (should i handle this or the IntentService will stop by itself in my case). also is cancelling the timer enough? (when the user signs in again, the IntentService will be called again so is that good?)
you should use the stopping method whenever you want to stop the service.
When ever you want to stop service just use "stopSelf();" method with in the service class.
By the way,the background services automatically destroyed when the application is removed from Ram.But You want to end the service with in the application then use "stopSelf();" method.
I hope this will help you.
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! :)
My android application starts a service in the onCreate() callback of a class that extends Application. The service performs some background tasks that are relevant to the user only while the application is running. For that reason I would like to close the service when the application's last activity is closed. I've tried to perform closing the service in the callback onTerminate() , but it never gets called . So what would be the best place where a service should be closed ?
Thanks !
An Android service, once started, will continue running until the Context.stopService() or stopSelf() is called.
There are various hooks you can use to stop the service using Context.stopService (the service itself, or an onDestroy()/onPause callback in one of the activities, or a button click).
It's true that Android does some resource management itself, but it can take a long time before Android decides to terminate your services. And a service that's running but not doing anything just consumes resources on the phone that other apps might need.
In your case, the onPause method of your last activity would be a good that will get called, and as such is the correct place to stop the service.
The onPause() callback will be made when your activity is paused for any reason, and you know that when this happens your app will not be visible again until onResume() is called. If your service has a reason to run in the use case that your activity might be started again soon, you should add an entry to your service that onPause() calls, to set a delayed service termination. In onResume() you can cancel that delayed termination through another entry.