I have developed an android 2.1 application. In that I am using AsyncTask for doing background processing. It gets the data from the WebService.
The Problem is : When I leave the application, the background service remains active only.
When I go to Settings->Applications->Running Applications it shows me that the application is running. I have to forcestop that application.
Is there any way to stop the background tasks automatically when user leaves the application ?
AsyncTask has a cancel method (http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean) )
Invoke it when your Activity is going to stopped (onStop) state, or maybe paused - onPause depending on your needs, see Activity lifecycle for further explanation here: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Related
I want to run a network request once user closes an app i.e. onStop and onDestroy is called on app's activity. The problem is I also want to wait for response from server and save some data locally after I get it so the whole procedure may need to be finished after the app is fully closed.
Is there any kind of background service in Android which won't be terminated after app is closed and will be waiting for requests's completion (for some reasonable time at least)?
The solution is Non UI fragment. Here is an example I have created:
https://github.com/kadymuhammad/Non-UI-Fragment .
You can also see this answer https://stackoverflow.com/a/21336010/5186466
Using Asynctask inside a Non UI fragment will continue to run even if the activity is paused but i will stop if the app task stopped.
The perfect solution is to use Service or IntentService classes.
Check this out :
http://www.tutorialspoint.com/android/android_services.htm
I am developing an application for a recognized financial institution. It is very important for them to manage security, and one of the requirements is that the application cannot run in the background. It was specifically requested that if a phone call is received, the application must get killed.
I tried using BroadcasterReceiver, by starting an activity when I hang up a call but apparently it runs as a service, and while my application is no longer running the activity is always started.
Is there any way to avoid background processing, like in iOS?
onPause() is called when your activity is going into the background.
If you overrode onPause() and stopped any sensitive activity in there, you would always catch the Activity going into the background.
im currently developing an app which plays the steam audio using MediaPlayer class. And i'd declare its main (Player) activity as SingleTop. Also on button "Back" it does moveTaskToBack(true) which acts the same as button Home does. So it just stays and plays on background and if user wants to see the gui he just starts the app once again (which is less convenient) or he clicks the special app's notify. Exit provided via menu.
but what are the benefits of using service instead of activity in such case? Definitely it would be more complex to develop, i have to say. Even instantiating the GUI while "on background" will take much more time, i'm afraid.
From the Android Documentation:
Activities
An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails.
Services
A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.
also
Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.
The Android OS can destroy your Activity if it runs out of resources, but it won't destroy the service.
EDIT: you should use startForeground() to ensure your Service won't be killed in situations where the resources are low. From the docs:
Make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. By default services are background, meaning that if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm. You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing.
How can a user destroy a service outside of the app. I made an app that the user can never "Exit" because the main activity disables the "back" button, but now my service notification can't be removed (there will eventually be conditions on when it is shown, but not right now)! I know that android manages the memory when the app is in the background like that, but if I really wanted to close that service, how would this be done?
You can use the Bindservice.The Service is stoped as the Bindservice.
i am working on an application that send and get data from internet each 5 min
if i press home key and my app goes to background... it will still continue sending/getting data from internet? or i have to do something special?
thanks
"If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere. If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state."
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
No. Activities shouldn't be depended on to process tasks in the background.
The following link illustrates the fundamentals of different Android components and what they do, i.e. the "parts" of an app.
You should be using a Service for background processing.
From what you're saying, I'd suggest an IntentService fired by an Alarm.
Application Fundamentals