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?
At the moment, I am leaning towards the following solution. Define
MyAsyncTask
without UI andMyUIAsyncTask extends MyAsyncTask
with UI; this achieves UI mode. DefineMyService
which has an instance ofMyAsyncTask
(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: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
andIntentService
) being activated at the time you need, and use that object within. Also, what kind of task are you planning on running in yourAsyncTask
?