Skip to content Skip to sidebar Skip to footer

Android Design Pattern For Background Operation With Ui And Non-ui Modes

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 opera

Solution 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?

Solution 2:

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.

Post a Comment for "Android Design Pattern For Background Operation With Ui And Non-ui Modes"