Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In my opinion this is the most tricky/hard part of a mainstream/average Android development. For instance on BlackBerry this is IN TIMES easier.</p> <p>Definitely you need to use a <code>Service</code>.</p> <p><code>AsyncTask</code> does not suit, because it is tightly "bound" to your <code>Activity</code> via a <code>Context</code> handle (otherwise you would not be able to update UI of the <code>Activity</code> from your <code>AsyncTask</code>). However an <code>Activity</code> can be killed by OS once the <code>Activity</code> went in background. An example reason of going to background can be an incoming call - user switches to Phone application so your <code>Activity</code> becomes invisible. In this case (depending on the current RAM state) OS may decide to kill one of the background (invisible to the user) activities.</p> <p>Some devs workaround this by arranging a static stuff for having a long-running actions inside of. Some recommend to use <code>Application</code> instance. This is because static stuff and <code>Application</code> exist while the whole app process exists. However those are incorrect workarounds. Processes in Android are also may be killed when OS decides it is time to. Android OS have its own considerations about what it can kill and in what order. All processes are devided to 5 levels of "killability". <a href="http://developer.android.com/guide/components/processes-and-threads.html#Lifecycle" rel="nofollow noreferrer">Here is the doc</a> where those levels are specified. It is interesting to read there:</p> <blockquote> <p>Because a process running a service is ranked higher than one with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply spawn a thread — particularly if the operation will likely outlast the activity. Examples of this are playing music in the background and uploading a picture taken by the camera to a web site. <strong>Using a service guarantees that the operation will have at least "service process" priority, regardless of what happens to the activity.</strong></p> </blockquote> <p>Your <code>Activity</code> where users initiate a long-running action should show a <code>ProgressDialog</code> to make sure user does not do anything else while the action is running. The guide is <a href="http://developer.android.com/guide/topics/ui/dialogs.html#ProgressDialog" rel="nofollow noreferrer">here</a>.</p> <p>Also, you'd most likely want to use the <code>NotificationManager</code> for notifying the user about your long-running action completion (or failure) if your <code>Activity</code> is currently invisible. Here is the <a href="http://developer.android.com/guide/topics/ui/notifiers/notifications.html" rel="nofollow noreferrer">NotificationManager info</a> to start from.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload