Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Service or IntentService for my android app?

Please correct me If I am wrong :

1) A Service is used to perform long tasks in background. A service runs in the UI thread so if there comes a long task then it may freeze our UI. A service will continue to run independent of application as long as we tell it to stop.

2) An IntentService on the other hand is used to perform short tasks in separate thread. It automatically terminates when it finishes its task.

What I have to do :

1) check for location every 5 seconds

2) if there is a change in location send it to server and Update the UI with new location values

What confuses me :

Should I use a Service or IntentService as I need to do it continuously after 5 seconds and does not want my UI thread to become unresponsive.

This app will be used to track a vehicle.

like image 675
Rakesh Avatar asked Feb 26 '14 08:02

Rakesh


People also ask

What is the main difference between a service and an IntentService?

Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service. IntentService creates a queue that passes one intent at a time to onHandleIntent(). Thus, implementing a multi-thread should be made by extending Service class directly.

When should I use a service in Android?

Services in Android are a special component that facilitates an application to run in the background in order to perform long-running operation tasks. The prime aim of a service is to ensure that the application remains active in the background so that the user can operate multiple applications at the same time.

What is the purpose of IntentService?

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent, in turn, using a worker thread, and stops itself when it runs out of work.


3 Answers

I wouldn't use an IntentService because it finishes itself once the job is done and you would need to re-schedule it again after 5 seconds. To re-schedule it you would need either some complicated external Timer mechanism associated with an application Context or, even worse, use AlarmManager that will suck your battery like crazy.

I would use a Service with a Timer inside for scheduling TimerTasks each 5 seconds and on each TimerTask that anyway executes on a worker thread I would get the position and make an Http request.

Just don't forget to cancel the timer on Service's onDestroy method otherwise you'll leak the Service instance.

EDIT I just noticed this and Update the UI with new location values ... Keep using the Service, but either use an AsyncTask for sending the request in doInBackground and then send a broadcast message in onPostExecute, either keep using the same TimerTask mechanism but use a Handler that is instantiated with a UI Looper and make UI update requests on that handler.

like image 148
gunar Avatar answered Nov 08 '22 16:11

gunar


1) A Service is used to perform long tasks in background. A service runs in the UI thread so if there comes a long task then it may freeze our UI.

yes, but when you use service for some long task then you create thread inside it. One very important feature of services is that android system is less likely to kill your app if your if there is low memory. And if you use setForeground then it will kill your app even more less likely.

A service will continue to run independent of application as long as we tell it to stop.

it will run in your application process, so you cant tell it will run inependently, you can of course stopp it from your app, or service can stop itself if it finished its work.

2) An IntentService on the other hand is used to perform short tasks in separate thread. It automatically terminates when it finishes its task.

IntentService extends Service and makes implementing Service a lot easier, it runs your code on its own worker thread. It will terminate if there is no more queued work to do.

Should I use a Service or IntentService as I need to do it continuously after 5 seconds and does not want my UI thread to become unresponsive.

it greately depends on your task, if your job is triggered from UI by some function then go with IntentService. Bare services are rally for really long tasks, ie. like mp3 players, or if you need constant communication with server.

If your task can be issued while your app is in background, ie. it is triggered by BroadcastReceiver, then you should look into wakefull intentservice:

https://github.com/commonsguy/cwac-wakeful

it will keep your app alive for the time intentservice will process your job, otherwise android is allowed to kill your app (after broadcast onReceive returns).

like image 43
marcinj Avatar answered Nov 08 '22 15:11

marcinj


It should be IntentService which you can schedule using AlarmManager

I have done something similar in this project:

https://github.com/madhur/MapMyLocation/tree/develop/src/in/co/madhur/mapmylocation

You may check it out

like image 1
Madhur Ahuja Avatar answered Nov 08 '22 15:11

Madhur Ahuja