2

I'm totally new to Android and I'm developing my first app. In this app I only use Internet twice a day. The first time, at the beginning of the day , when the user downloads all the necessary data to work during the day , and the second time at the end of the day to send all the results of the day's operations to the server(to sync data)

I know that I should use a different thread for operations that are known to take some time, like downloading data from the Internet.But what happened to the AsyncTask when the user is downloading data or sending data to server , and he/she receives a phone call?

For other operations in my app, it doesn't matter if I need to start all over again, but for these two operations that use the Internet I don't think it would be a good idea.

So knowing that a Service will continue to run no matter what happens to the Activity, the question is, what should I use for these two operations that use the Internet? an AsyncTask or a Service with a Thread??

Thanks in advance.

eddy
  • 4,373
  • 16
  • 60
  • 94

2 Answers2

3

You can use IntentService, which is a service that already implements a background thread for executing long lasting tasks. One positive aspect of IntentServices is that by default they automatically stop at the end of execution of the task and release resources used. All you have to do is overwrite the onHandleIntent() method which is executed on the background thread for downloading the necessary tasks:

@Override
protected void onHandleIntent(Intent intent) {

    // download tasks here
}

For details you can check the android developers website here

**

EDIT:

**

Updating the UI components from a Service can be easily achieved by using LocalBroadcastManager which sends broadcast Intents withing components of one Process. See this answer for details.

Community
  • 1
  • 1
eldjon
  • 2,800
  • 2
  • 20
  • 23
  • Thanks for your suggestion. I also think that using an IntentService might be the best choice, BUT what can I do if I need to somehow update the UI. For example when all this processing is taking place in the background I need to show some kind of indicator to the user, a ProgressDialog perhaps. Then when all the background processing is done, close the ProgressDialog. This would be easy using a AsyncTask since I can nest the class that extends AsyncTask within the Activity I need to update – eddy Aug 31 '14 at 23:02
  • just to clarify sth: the AsyncTask in an Activity does not stop its task when you receive a phone call. – eldjon Aug 31 '14 at 23:43
  • It doesn't? Then what difference does it make using a Service instead of a AsyncTask? – eddy Aug 31 '14 at 23:51
  • if a phone call is received the onPause()/onStop() of the activity will be called. if you dont explicitly cancel the task, it will continue to run. AsyncTask however is intended to execute short tasks (few seconds) thats why the service is more adequate – eldjon Aug 31 '14 at 23:54
  • I see. The problem is that I can't find any example of how to update the UI from a service – eddy Sep 01 '14 at 00:00
  • create an interface and have the activity implement it. bind the activity to the service and on onServiceConnected() set the listener to your service. so while running the service can notify the listener which in this case would be your activity – eldjon Sep 01 '14 at 00:09
  • So the only choice is a Bound Service? I guess I'll have to read that article since I never thought I'd go as far as I having to use a Bound Service in my very first app :S – eddy Sep 01 '14 at 00:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60355/discussion-between-eldjon-and-eddy). – eldjon Sep 01 '14 at 01:23
  • Please take a look at this question http://goo.gl/i9z5nk I'd really appreciate your point of view – eddy Sep 02 '14 at 14:23
1

I will use a Service in foreground mode, because it will be not killed by system when low on memory. You can follow this link for more info.

JJ86
  • 5,055
  • 2
  • 35
  • 64