Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42642

Is it better to use AsyncTask (Or Timer) within single process, or using Service in separate process?

I just read Android Architecture Tutorial: Developing an App with a Background Service (using IPC). It is basically

  1. Have a service run in separate process.
  2. A repeated timer event will occur in the service.
  3. Within the timer event handler, it will perform networking to retrieve tweet, and inform all the listener attached to it. Listeners are attached to it through IPC.

I can see there are 2 major characteristics with this approach.

  1. Tweet retrieving action run within separate process.
  2. It always run, even the main activity has quit.

However, if "It always run" is not my requirement. I want everything to stop when I quit my main Activity.

  1. Will it be better, if I use AsyncTask (Or Timer) within my main Activity, to perform tweet retrieving action? Everything will be run within single process. No more using Service.
  2. Using AsyncTask (Or Timer), seems simpler. We no longer need to deal with IPC.
  3. Or, using Service approach might be better? Am I missing some goodies provided by Service?

Upvotes: 4

Views: 236

Answers (4)

Kooki
Kooki

Reputation: 1157

At first you have to know, that a service isn't a Thread. If a activity binds a Service and runs as a Deamon, but a ASynchTask is another thread.

ASynchTask's are designed for doing some work which should not running on UI-Thread (for example processing some larger calculations)

Services are designed to run permanantly on Background. If you want to permanantly check for new tweets, even if your activity is stopped or paused, you should use a Service, which checks into an own thread for new data.

TimerTask are good old java style implementations which run on their own thread. You can used them for processing some data, but you'll have some problems to manipulation UI. If you want to be it on propper "AndroidWay", use a Handler instead of TimerTask.

Upvotes: 1

Jameo
Jameo

Reputation: 4607

First of all, I know the tutorial you are following...I've followed that tutorial myself while trying to learn IPC. One thing you need to know is, The Android docs explicitly say,

Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service.

If at all possible, you should just bind to the service.

Also, you must consider, do you really need a service? Consider that the Android Twitter app doesn't even refresh tweets for you, its on an as needed basis. Polling can be battery intensive, so you must consider if this is really necessary.

Also, will you be using these tweets from multiple activities? If so it would be nice to not duplicate the code in multiple places. So maybe you do want a service in this case.

Other than that, I would recommend that you start simple (Async task with a timer to update it), and move to a service if you think you need it.

Upvotes: 0

ScouseChris
ScouseChris

Reputation: 4397

I would take the view that a TimerTask can be set to execute and repeat at a given interval, Timers run on a separate thread so all this work would occur in the background without disturbing the UI. It would be easy for you to trigger an update within your app when the TimerTask completes and update the UI when you want.

When you exit the app it's a simple case of calling cancel() on your Timer and the purging all the tasks with purge().

Nice and easy and you don't need to implement IPC, which can be very fiddly to get right.

EDIT

Using AsyncTask you can do pretty much exactly the same thing but you'll have to manually schedule the next run. I have used both solutions in the past and found them to work equally well so it's all down to your preference.

Upvotes: 1

Murtuza Kabul
Murtuza Kabul

Reputation: 6514

Using service is a better approach as it will allow you to perform the polling independent from the application flow.

As it is a task where no user interaction is required and it has to be done in the background without disturbing the main UI of application and whatever the user is doing, a service is an ideal candidate for its implementation.

It is possible to bind the service with the application in such a way that when the main application terminates, it will also turn off the service.

Upvotes: 1

Related Questions