0

I have a background operation that I'd like to be able to run in two modes:

  • UI mode, which is launched by the user and provides the user precise feedback on the state of the operation throughout its life cycle.

  • non-UI mode, which is launched by AlarmManager and provides the user with a summary of the operation at the end of its life cycle.

The natural design choice to achieve only UI mode would be AsyncTask and the natural design choice to achieve only non-UI mode would be IntentService.

What is the natural design choice to achieve both modes simultaneously? I.e., what is the natural design choice to incorporate these two modes into a single object?

user2768
  • 794
  • 8
  • 31

2 Answers2

1

Instead of having an object doing the same stuff through different ways (UI / non-UI), I would move the business logic into a separate class, then have two different objects (AsyncTask and IntentService) being activated at the time you need, and use that object within. Also, what kind of task are you planning on running in your AsyncTask?

mbmc
  • 5,024
  • 5
  • 25
  • 53
  • The `AsyncTask` will be fetching web-content. The amount of content will depend on several factors and may take a second-or-so, or considerably longer. I am considering implementing a "cancel and schedule to run in the background" option, to enable the user to decide when they have had enough of waiting. – user2768 Aug 14 '14 at 00:13
  • If you need to fetch web data, I'd recommend using [Picasso](https://github.com/square/picasso), [loopj](http://loopj.com/android-async-http/) or [RoboSpice](https://github.com/stephanenicolas/robospice). There's a bunch of considerations with an `AsynTask` (handling screen rotation, holding onto an `Activity` etc). – mbmc Aug 14 '14 at 00:19
  • Those libraries look interesting, but they don't seem powerful enough (by themselves), in particular, we need to access and manipulate the DOM. – user2768 Aug 14 '14 at 00:33
0

At the moment, I am leaning towards the following solution. Define MyAsyncTask without UI and MyUIAsyncTask extends MyAsyncTask with UI; this achieves UI mode. Define MyService which has an instance of MyAsyncTask (see [Is it possible to use AsyncTask in a Service class?); which achieves non-UI mode. I'm not convinced that this is the best solution, moreover, it violates the following threading rules:

  • The AsyncTask class must be loaded on the UI thread.
  • The task instance must be created on the UI thread.
  • execute(Params...) must be invoked on the UI thread.
Community
  • 1
  • 1
user2768
  • 794
  • 8
  • 31
  • It seems that launching `MyAsyncTask` using `AlarmManager` is ill-advised: http://stackoverflow.com/questions/25174226/is-instantiating-an-asynctask-from-a-wakefulbroadcastreceiver-recommended. – user2768 Aug 13 '14 at 23:45
  • Hi Alarm manager is different it extends broadcast receiver and intent in broadcast stays for few seconds only and so only short running process are advised while your service runs on UI thread till you don't fork a different thread so you can always start an asynctask from it – user1530779 Feb 05 '15 at 04:20