Android design pattern for background operation with UI and non-UI modes

前端 未结 2 1697
猫巷女王i
猫巷女王i 2021-01-26 06:46

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 preci

2条回答
  •  情话喂你
    2021-01-26 07:40

    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.

提交回复
热议问题