在Android中使用MVP模式时,应该在哪里调用android服务并调用GoogleAPIClient?

时间:2016-01-13 15:57:35

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

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

我已成功实施 view / presenter / interactor 类。

我不清楚
  • 在何处放置service来电代码?
  

因为我无法在演示者或交互者中获取上下文   上课时,我无法将service电话放在那里

  • 在哪里实施GoogleApiClient课程?
  

由于GoogleApiClient还需要运行上下文,因此它也不可能   在没有上下文的演示者或交互器中实现

2 个答案:

答案 0 :(得分:7)

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

我试图在没有匕首的情况下实现它。但这似乎违反了MVP架构。

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

<强>活动

Vendor

<强>演示

Buyers

演示者实施

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();
}

交互式实施

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();
   }
}

答案 1 :(得分:0)

我也在寻找你的第一个问题。但是,我有第二个问题的答案。

答案是Dagger2。 (http://google.github.io/dagger/)您可以使用Dagger2轻松注入GoogleApiClient对象。