Android Design Considerations: Asynctask VS Service (Intentservice)

Android: Design for concurrent background jobs (Service, IntentService or AsyncTask?)

  1. I would move the download task into a service.
  2. I would not implement it myself, instead I would use the DownloadManager System service, see doc and usage example.
  3. In your UI you can query the download status inside a background thread, see this SO question: Show Download progress inside activity using DownloadManager. Different to the solution I would use an AsyncTask instead.

This allows you to perfom downloads in parallel. In the activity you can show the current progress by polling in a background thread, but I am not sure what you mean with the notification? The DownloadManager shows a notification when starts with downloading.

Your downloads will be much stabler instead of using an own implementation, since DownloadManager can deal with connection loss and will perform a retry.

what should I use for syncing database server to local? IntentService or AsyncTask

Out of IntentService and AsyncTask, IntentService is better suited for this purpose. AsyncTask cannot live on its own and needs to be started from an Activity or a service. Also, in this case, you do need to change the UI on task completion and hence AsyncTask is out of the question.

For data sync, you can also look into WorkManager/JobScheduler. It has nice to use APIs and will provide you with better sync options like : Sync when connected to WiFi, or sync when connected to charger etc.

AsyncTask executed from Service (IntentService) and Activity - is there a difference?

AsyncTask isn't really appropriate for things where you're concerned about surviving outside the lifecycle of a component (if you need to message back to that component). If you're going to the extent of having a service, I wouldn't even bother with AsyncTasks. As far as parallel or queue, that really depends on a lot of different variables, but I certainly wouldn't make it completely parallel for any number of downloads/uploads. I'd set some limit on the max number of concurrent transfers.

There's no point in using IntentService for parallel exeuction--you've noted the problem with that already. You're getting into territory which is not really covered by the Android API. AsyncTask and IntentService are nice abstractions that make several scenarios easy, but parallel execution of many many tasks is not one of them. It's probably best to use some of the Java threading/concurrency classes. Take a look at ThreadPoolExecutor.

Best practices of notifying UI components from AsyncTask or service

If the component to update is the component that starts the update Job (through AsyncTask or Service), you should probably use an inner AsyncTask

AsyncTask offers you two posibilites to update the UI :

  • To update the UI in parallel with the task executed in doInBackground() (e.g. to update a ProgressBar), you'll have to call publishProgress() inside the doInBackground() method. Then you have to update the UI in the onProgressUpdate() method.
  • To update the UI when the task is done, you have to do it in the onPostExecute() method.

see :

doInBackground()

publishProgress()

onProgressUpdate()

onPostExecute()

Edit :

public class Home extends Activity implements OnClickListener {
private Button mButton;
private TextView mTextView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
mButton = (Button) findViewById(R.id.myButton);
mTextView = (TextView) findViewById(R.id.myTextView);
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.myButton:
(new MyAsyncTask()).execute();
break;
default:
break;
}
}

private class MyAsyncTask extends AsyncTask<String, int[], Boolean> {

/** This method runs on a background thread (not on the UI thread) */
@Override
protected String doInBackground(String... params) {
for (int progressValue = 0; progressValue < 100; progressValue++) {
publishProgress(progressValue);
}
}

/** This method runs on the UI thread */
@Override
protected void onProgressUpdate(Integer... progressValue) {
// TODO Update your ProgressBar here
mTextView.setText("Updating : " + progressValue + "/100");
}

/**
* Called after doInBackground() method
* This method runs on the UI thread
*/
@Override
protected void onPostExecute(Boolean result) {
// TODO Update the UI thread with the final result
mTextView.setText("Update complete !");

}
}
}

You can find another example here.

Considerations when adding a service to an existing android project?

First of all, please keep in mind that 'giant god' classes are not always atrocious in Android.

Since there's no way to identify what Service you are talking about, I think you need to get a better understanding on whether should a Service or a AsyncTask run.

Service

A Service is a part of your Application that has no UI. It may be
called by a UI(Activity) to be started, or may be started by any other
component of your Application. When developing, you have the freedom
to place it on a different thread, or even run it in a different Task
or Process. This allows you to ultimately separate it from your UI.
Additionally, you may start the Service to run independently
(startService) or bind your activity to it (bindService) depending
upon your needs. By using custom Handlers, you can set callbacks to
update the UI with your progress. A Service does not necessarily end
if a User changes Activities, but may be ended at ANY time by the OS. [1]

AsyncTask

A AsyncTask is always instantiated from the UI thread. It only allows
specific callbacks, but simplifies the process of multi-threading for
the purposes of relatively short transactions (as compared to
dedicated separate threaded services) that are inherently tied to
actions performed by an Activity. Whenever a User changes Activities,
the AsyncTask is put on "pause" and may even die because there is no
UI thread for your Activity any longer.
[2]

Thus, sometimes you will handle situations where you won't have to use neither.



Related Topics



Leave a reply



Submit