android - 在android中使用MVP模式时,android服务调用和对GoogleAPIClient的调用应该写在哪里?

标签 android design-patterns android-service mvp android-googleapiclient

我正在尝试通过引用此链接在我的 android 项目中实现 MVP 模式:https://github.com/jpotts18/android-mvp

我已经成功地实现了view/presenter/interactor 类。不清楚

  • 服务调用代码放在哪里?

Since i cannot get the context inside the presenter or interactor class, I am not able to put the service call there

  • 在哪里实现 GoogleApiClient 类?

Since GoogleApiClient also requires context to run, it also cannot be implemented inside the presenter or interactor without a context

最佳答案

使用 dagger 可以更轻松地将 Interactor 注入(inject) Presenter。试试这个链接(https://github.com/spengilley/AndroidMVPService)

我正在尝试不使用 Dagger 来实现它。但这似乎违反了 MVP 架构。

我从 Activity 创建了一个 Interactor 实例。然后使用 Interactor 作为参数之一创建 Presenter 实例。

Activity

public class SomeActivity extends Activity implements SomeView {
   private SomePresenter presenter;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      SomeInteractor interactor = new SomeInteractorImpl(SomeActivity.this);
      presenter = new SomePresenterImpl(interactor,this);
   }

   @Override
   protected void onStart() {
     super.onStart();
     presenter.startServiceFunction();
   }

演示者

public interface SomePresenter {
   public void startServiceFunction();
}

Presenter 实现

public class SomePresenterImpl implements SomePresenter {
   private SomeInteractor interactor;
   private SomeView view;
   public SomePresenterImpl(SomeInteractor interactor,SomeView view){
      this.interactor = interactor;
      this.view = view;
   }
   @Override
   public void startServiceFunction() {
      interactor.startServiceFunction();
   }
}

交互者

public interface SomeInteractor {
   public void startServiceFunction();
}

交互器实现

public class SomeInteractorImpl implements SomeInteractor {
   private Context context;

   public SomeInteractorImpl(Context context) {
      this.context = context;
   }

   @Override
   public void startServiceFunction() {
      Intent intent = new Intent(context, SomeService.class);
      context.startService(intent);
   }
}

关于android - 在android中使用MVP模式时,android服务调用和对GoogleAPIClient的调用应该写在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34771255/

相关文章:

java - 如何将字符串从 UDP 监听服务传递到 MainActivity?

android - 选择性 Google Play 服务 API 找不到类

php - 两个交互类的 OOP 设计方法

java - Setters AND(不是 OR 或 VS)构建器模式

C++ 接口(interface) : how to avoid typing the same method name 3 times?

java - TTS 无法从 Android 中的 IU 线程正确循环?

java - RxJava 没有重复的合并

android - strings.xml 与静态常量

android - onServiceConnected 未被调用,出现空指针异常

android - 访问服务的变量