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.
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?
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 have a class that implements fileuploader service. Activites bind to it and supply it a list of files to be uploaded, and then unbind immediately. The files are then uploaded one-by-one by the service in a background thread(asynctask).
I start this service in my dashboard actvity using startService(), so that it keeps running until specifically stopService() is called.
Now, my question is when do I stop this service?
Basically I need to check two conditions: 1: all files are uploaded; 2: app has exited.
Also, to exit the App, user has to press back button on dashboard activity.
I could have overrided back button press and queriesd the service whether any files are left, but I dont want to use that method.
Any suggestions?
Activites bind to it and supply it a list of files to be uploaded, and then unbind immediately.
I would recommend then using startService() rather than bindService().
The files are then uploaded one-by-one by the service in a background thread(asynctask).
This seems like a much better fit for startService() and an IntentService (or a WakefulIntentService perhaps, if you are concerned about the device falling asleep during uploads).
I start this service in my dashboard actvity using startService()
This would not be needed if you used startService() for sending over the work.
so that it keeps running until specifically stopService() is called.
Ideally, the service would shut itself down, like IntentService does. After all, only the service knows when the service is done.
my question is when do I stop this service?
When you have no more work to do. IntentService does this automatically. If you really want to maintain your own thread pool for doing the work, then when your work queue is empty and all threads are done, call stopSelf() from within the service.
Basically I need to check two conditions: 1: all files are uploaded
Yes.
2: app has exited
No. Your UI should not care whether the service is running or not running. The service should take care of itself.
Also, to exit the App, user has to press back button on dashboard activity.
Users are welcome to leave your app however they please: BACK, HOME, RECENTS, a Notification, an incoming phone call, etc.
Any suggestions?
Use an IntentService. Send over the jobs to be uploaded via calls to startService(), packaging all needed data into the Intent used with startService() (e.g., extras). Do your upload work in onHandleIntent(). If desired, use LocalBroadcastManager to let activities in your app know about the upload status, so they can reflect that in their UI if they so choose. IntentService will handle stopping itself when its work queue empties.
I have a user login screen. The user presses a login button and I create an IntentService to connect to my rest services and return back a authentication result.
Now here's the functionality I want to achieve: If the activity is paused (i.e. goes into the background) then the intent service should still run, and it does. But if I use the task manager to kill the application, the intent service should stop, although right now it doesn't. It continues executing onHandleIntent until complete. If I manually call stopSelf(), onDestroy() is called but the onHandleIntent method continues to execute. How do I force the onHandleIntent to stop? Calling "return" is not an option because it could be caught up in one rest method call.
Should I be using intent service for this functionality or something else (like service or asynctask)?
First off, if your service is doing something that you want done while your app is running in the foreground, then you don't need a Service. Your activity or dialog can just spawn a thread to do your task, and these threads will end when your app ends, since they are within the task scope of your app. Otherwise, for tasks you want done all the time, when the user could be doing anything on the device, definitely use a Service.
I'm not sure why "calling return" is not an option for you? By this I assume you mean checking for a condition, and exiting the loop or block if the condition is met. This is generally how threads (in services or elsewhere) should end (although there are other approaches). Your service's thread can listen to a specific event that has occurred (like if the activity has been destroyed, by setting a property in a file or database when the activity's onDestroy is called, though if this is what you want then you probably should NOT use a Service), and then run() can return gracefully.
But if this is a task you want done no matter what the user is doing on his device (within or outside of your app), then it should be a task that should not be interrupted from the outside. It should keep going until it's done.
Maybe if you could better define when you want this service to be stopped?
stop it in your onDestroy() method
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.