本文介绍了Android服务应在MVP模式中扮演什么角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个进行人类活动识别的Android应用。

I am developing an Android app that does Human Activity Recognition.

基本上是这样的-服务不断读取加速器数据并存储识别出的活动(例如,步行,正在运行)。用户可以在活动的 ListView 中查看所有识别的活动(访问数据库)。数据库中的每个User表都有一个pa_goal(体育活动目标)字段,服务会从数据库中读取pa_goal字段并进行一些检查。

It basically works like that - Service constantly reads the accelerator data and stores the recognized activity (i.e. Walking, running) in a database. The user can see all of the recognized activities in an ListView in activity (accesses the database). Every User table in the database has a pa_goal (physical activity goal) field which the Service reads from the database and does some checks.

用户当然可以更改这个目标来自一项活动。因为我将实现MVP架构模式。

The user, of course, can change this goal from an activity. Since I will be implementing the MVP architectural pattern.

我不确定将服务放在哪里?当然不是View。有任何建议吗?

I am unsure where to put the Service? It surely isn't View. Any advice?

推荐答案

在干净的体系结构中(这是我假设您正在使用MVP的思想),将框架与业务逻辑分开。这本质上是普通演示者允许您执行的操作。

In a clean architecture, which is what I am assuming you are using MVP for, there is the idea of separating the framework from the business logic. This is essentially what a normal presenter allows you to do.

在这种情况下,它不是您要处理的视图,但是原理相似。您不希望将所有业务或应用程序逻辑混在Android代码中,因为您可以将它们分开,以获得更好,更单一的职责类别。因此,我想说的是,尽管它不是一个视图,但您仍然应该具有一个主持人类型的类(也许最好称为控制器或管理器)。

In this case its not a view you are dealing with but the principle is similar. You don't want all your business or application logic mixed in the Android code when you can separate them out for nicer, more single responsibility classes. So I would say that while it isn't a view you should still have a presenter type class (probably better to be called controller or manager maybe).

此类将是一个POJO,用于控制服务的行为方式,可以使用标准的junit测试和服务模拟轻松地对其进行测试。然后,可以将此类和服务放入其自己的功能包中,并以与演示者相同的方式与后端模型进行交互。

This class would be a POJO that controls how your service behaves which is easily testable with standard junit tests and service mocks. This class and the service could then be put into its own feature package and interact with the back end models the same way as your presenters.

因此,总的来说,角色是应用程序的另一个功能,该功能与其他功能(通常只是我的经验)一起出现。

So, in summary, the role is of another feature of your app that sites alongside the other features (which are usually just views in my experience).

希望有帮助

这篇关于Android服务应在MVP模式中扮演什么角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:57
查看更多