Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use and when not to use a Service in Android

Tags:

I have been developing for Android for little less then 2 years, and I am still puzzled by this seemingly simple question. When should one implement a service? From my experience there are some rare cases but I am questioning this because on every phone there are quite a lot of them running and I doubt it's just a poor application design.

This is essentially core of my question but following are some of my experiences and thoughts about the subject which can explain my question in more detail.

In all apps that I have developed only one really required a service. It was a background sound recorder and I was using it as Foreground service with notification since I wanted buttons to be able to control it (like music players do for example).

Except this I never really saw a requirement for the constantly running service because:

A) Intent listeners (Manifest registered BroadcastReceivers) are quite a useful feature and using them as you know is usually enough for many use-cases (for example showing notifications).

B) If scheduled execution is a must one can subscribe to alarm events.

C) I know that service in Android is quite different then for example in Windows since in Android services are just a "package" to organize your code in and have a the system manage the lifetime of the object. Services use the Main Thread but it's customary to spawn new threads in them.

D) In the development documentation services are suggested for network communication and background calculations but I don't get why you should not just use AsyncTasks for that. I am a big fan of these and use them extensively for lot of things from downloading data from the internet to doing FFT calculations under time critical conditions.

E) I get the usefulness of Foreground services but why are people using background services so much (excluding the system apps).

Those are my thoughts about the SERVICE and I hope someone with more experience will be able to explain these PROS and CONS (along with others that I probably missed).

like image 494
Igor Čordaš Avatar asked Jan 15 '14 21:01

Igor Čordaš


People also ask

When should I use a service in Android?

If you want to do any work off the main thread while your app is active(like sound of game), use thread, but if your operation needs to keep running even when your app is in background(like your music player), you must use service.

Which case you'll use service and in which case you'll use thread?

A thread should be used in a long running process that would block the UI from updating. If it's more than a second or two you might want to put it into a background thread and notify the user with a dialog or spinner or something.

What is the use of Android service?

Android service is a component that is used to perform operations on the background such as playing music, handle network transactions, interacting content providers etc. It doesn't has any UI (user interface). The service runs in the background indefinitely even if application is destroyed.

What are the two types of services in Android?

Android services life-cycle can have two forms of services and they follow two paths, that are: Started Service. Bounded Service.


Video Answer


1 Answers

When should one implement a service?

When you have work -- delivering value to the user -- that:

  • Needs some time to complete, perhaps longer than you have time for in the component wishing the work to be done, or

  • Is delivering that value under user control (e.g., music player, controlled by play/pause buttons in a UI), or

  • In rare cases, needs to be running continuously, as it delivers value continuously

there are quite a lot of them running and I doubt it's just a poor application design

Some are likely to be poor implementations, either due to technical misunderstandings, or other concerns (e.g., making marketing happy) trumping making users happy.

It was a background sound recorder and I was using it as Foreground service with notification since I wanted buttons to be able to control it (like music players do for example)

That is a reasonable use for a service, IMHO.

Intent listeners are quite a useful feature and using them as you know is usually enough for many use-cases (for example showing notifications)

I assume that by "Intent listeners" you mean manifest-registered BroadcastReceivers. In that case, if the work to be done by the BroadcastReceiver will take more than a millisecond, that work should be delegated to an IntentService for completion. onReceive() is called on the main application thread, and it is not safe for a manifest-registered BroadcastReceiver to fork a bare thread, as the process could go away shortly after onReceive() returns. However, in these cases, the service is usually short-lived (e.g., do some network I/O and disk I/O, then go away).

In the development documentation services are suggested for network communication and background calculations but I don't get why you should not just use AsyncTasks for that

An AsyncTask is a fine solution for background work that is:

  • Requested by the UI (activity or fragment), and

  • Will take less than a second or so, and

  • Is non-critical

For example, if you are downloading avatars to show in a ListView, AsyncTask is probably a fine choice, whether you use them directly or use some image-fetching library that uses them internally.

Conversely, if the user buys an MP3 through your app, and you need to download that MP3 file, an AsyncTask is not a good solution. That could easily take over a second. While the download is going on, the user could switch away from the app (e.g., press HOME). At that point, your process is eligible to be terminated... perhaps before your download is complete. Using an IntentService to manage the download is a signal to the OS that you are really doing work here, adding value to the user, and so the process will be left alone for a little while.

Note that if the background work might take 15+ seconds, WakefulBroadcastReceiver or my WakefulIntentService is probably a good idea, so the device does not fall asleep while you are trying to wrap up this bit of work.

like image 100
CommonsWare Avatar answered Oct 03 '22 01:10

CommonsWare