Is it better to use AsyncTask (or a timer) within a single process or use the Service in a separate process?

I just read the Android Architecture Tutorial: developing an application with a background service (using IPC) . It is basically

  • Start the service in a separate process.
  • A repeated timer event will occur in the service.
  • In the timer event handler, it will perform network interaction to receive tweets, and inform all listeners attached to it. Listeners connect to it through IPC.

I see that with this approach there are two main characteristics.

  • Tweet Getting the action is done in a separate process.
  • It always starts, even the main action is completed.

However, if β€œ He always works, ” this is not my requirement. I want everything to stop when I quit my main activity.

  • Would it be better if I use AsyncTask (Or Timer ) in my main activity to do a tweet extraction? Everything will be carried out in a single process. No longer use Service .
  • Using AsyncTask (or Timer) looks easier. We no longer need to deal with IPC.
  • Or, using the Service approach, could it be better? Are there any missing useful features provided by the Service?
+4
source share
4 answers

Using the service is the best approach, as it will allow you to perform a survey regardless of the flow of the application.

Since this is a task in which interaction with the user is not required, and this needs to be done in the background without violating the main user interface of the application and no matter what the user does, the service is an ideal candidate for its implementation.

You can associate a service with an application so that when the main application terminates, it also disables the service.

+1
source

You should know first that a service is not a thread. If the action binds to the Service and works like Deamon, but ASynchTask is a different thread.

ASynchTask are designed to do some work that should not be done in UI-Thread (for example, handle some larger calculations)

Services are designed for continuous operation in the background. If you want to constantly check for new tweets, even if your activity is stopped or paused, you should use the Service, which checks its own stream for new data.

TimerTask are old, old java-style implementers that run on their own thread. You can use them to process some data, but you will have problems manipulating the user interface. If you want it to be on the propper "AndroidWay", use a handler instead of TimerTask.

+1
source

I would suggest that a TimerTask can be set to execute and repeat at a given interval, Timer runs in a separate thread, so all this work will happen in the background without breaking the user interface. It will be easy for you to initiate an update in your application when TimerTask completes and updates the user interface whenever you want.

When you exit the application, this is a simple case of calling cancel() on your Timer and cleaning up all tasks with purge() .

It's nice and easy, and you do not need to implement IPC, which can be very difficult to get right.

EDIT

Using AsyncTask , you can do almost the same thing, but you will have to manually plan the next run. I used both solutions in the past and found that they work equally well, so it all depends on your preferences.

+1
source

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

Note. Using AIDL is only necessary 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 just need to bind to the service.

In addition, you must consider whether you really need a service? Think that the Android Twitter app doesn’t even update tweets for you, as well as when necessary. The survey can be intense, so you should consider whether it is really necessary.

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

In addition, I would recommend that you start simple (Async task with a timer to update it) and go to the service if you think that you need it.

0
source

All Articles