对于后台操作的Andr​​oid的设计模式与UI和非UI模式模式、后台、操作、Andr

2023-09-03 22:39:24 作者:不一样的风景

我有,我希望能够在两种模式下运行一个后台操作:

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

UI模式,这是由用户发起,并在整个生命周期中的运行状态,用户precise反馈。

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

非UI模式,由 AlarmManager 启动,并提供了与操作的总结用户在年底它的生命周期。

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.

自然的设计选择,只能实现的 UI模式的是的AsyncTask 和自然的设计选择仅实现的非UI模式的是 IntentService

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?

推荐答案

而不必对象通过不同的方式(UI /非UI)做同样的东西,我想将业务逻辑放到一个单独的类,然后让两个不同的对象(的AsyncTask IntentService )被激活,在您需要的时间,并使用内的对象。此外,你打算在运行什么样的任务,你的的AsyncTask

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?